Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Problem with inserting a record

Former Member
0 Likes
1,495

Hi Friends,

I want to update my database table with new record for this I wrote a coding like this, but i am unable to update the database table please find my coding:

LOOP AT lt_invmov INTO lw_invmov WHERE check = 'X'.

SELECT MAX( trans )

INTO zinv_mov-trans FROM zinv_mov.

IF sy-subrc EQ 0.

zinv_mov-trans = zinv_mov-trans + '0000000001'.

ELSE.

MOVE '0000000001' TO zinv_mov-trans.

ENDIF.

itab-lifnr = lw_invmov-lifnr.

itab-xblnr = lw_invmov-xblnr.

itab-bukrs = lw_invmov-bukrs.

itab-usnam = zinv_mov-usnam.

itab-appdate = sy-datum.

MOVE 'V2' TO itab-status.

MOVE 'CL' TO itab-clear.

APPEND itab.

INSERT INTO zinv_mov VALUES itab.

ENDLOOP.

ENDIF.

Regards,

Line

1 ACCEPTED SOLUTION
Read only

athavanraja
Active Contributor
0 Likes
1,470

1. refresh your itab just before the select max statement

2. to update the dbtable use modify stt.

modify zinv_mov from table itab .

this way even if a record exists in the dbatable with the same key it will not dump

Raja

9 REPLIES 9
Read only

Former Member
0 Likes
1,470

Hi Line,

Please write commit-work after insert statement.

Try it.

Reward if useful!

Read only

Former Member
0 Likes
1,470

Hi Line ,

Your code seems to be correct , what is the value of sy-subrc you get after the insert command.

I would also suggest you one thing , currently you are inserting the data into internal table itab and the inseerting the table with data in the header , what i would suggest is instead of using the insert command for each record , why dont you insert all the records at one go , after ENDLOOP.

Use the syntax

INSERT dbtab FROM TABLE itab .

Regards

Arun

Read only

0 Likes
1,470

Yes I am getting sy-subrc = 4 at the insert statement. please suggest me

Regards,

Line

Read only

0 Likes
1,470

Hi Line ,

So it basically means that record is not getting inserted into the table , please check if there is already a record with the same key feilds in the database.

Generally it is better to use modify command , as it will insert a new record if it is not there or modify the existing record , where as insert will try only to insert a record if a record with the same key already exists then it will give sy-subrc as 4.

Try with modify command and see.

Regards,

Arun

Read only

Former Member
0 Likes
1,470

do like this it may help you..

LOOP AT lt_invmov INTO lw_invmov WHERE check = 'X'.

SELECT MAX( trans )

INTO zinv_mov-trans FROM zinv_mov.

IF sy-subrc EQ 0.

zinv_mov-trans = zinv_mov-trans + '0000000001'.

ELSE.

MOVE '0000000001' TO zinv_mov-trans.

ENDIF.

itab-lifnr = lw_invmov-lifnr.

itab-xblnr = lw_invmov-xblnr.

itab-bukrs = lw_invmov-bukrs.

itab-usnam = zinv_mov-usnam.

itab-appdate = sy-datum.

MOVE 'V2' TO itab-status.

MOVE 'CL' TO itab-clear.

APPEND itab.

ENDLOOP.

INSERT INTO zinv_mov from table itab.

ENDIF.

your itab should be like zinv_mov .

regards

shiba dutta

Read only

Former Member
0 Likes
1,470

first check whether your itab is like dbtab or not? then confirm that whether key fields of the table are you filling or not ? I think you have to fill the values in key fields also you can not insert initial value in key fields.

regards

shiba dutta

Read only

0 Likes
1,470

Shiba,

Itab is like dbtab.

All the key fields are filling in the intenal table.

All the records are updated in the internal table but not in the database table.

Regards,

Line

Read only

0 Likes
1,470

i dont know why it is happening you can try

insert dbtab from table itab accepting duplicate keys.

if you are trying some duplicate keys to enter then it will not insert in the database table. only the previous value will be there for that key... Just check whether you are repeating the keys or not(may be already exist in the dbtab)? and use commit work after insert.

regards

shiba dutta

Read only

athavanraja
Active Contributor
0 Likes
1,471

1. refresh your itab just before the select max statement

2. to update the dbtable use modify stt.

modify zinv_mov from table itab .

this way even if a record exists in the dbatable with the same key it will not dump

Raja