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

Insert internal table into datbase table

Former Member
0 Likes
9,825

Hi,

I want to insert internal table into database table.

i have use following code

loop at it_header.

INSERT ZTAB from table it_header accepting duplicate keys.

endloop.

but above code insert only first line not multipal.

Thanks in Advance.

Sam

1 ACCEPTED SOLUTION
Read only

Mohamed_Mukhtar
Active Contributor
0 Likes
4,309

Hi,


"Dont write this statment in LOOP....ENDLOOP
INSERT ZTAB from TABLE it_header[]  ACCEPTING DUPLICATE KEYS.
IF sy-subrc = 0.
MESSAGE 'Data Successfully saved into Database table' TYPE 'S'.
ENDIF.

Thanks

9 REPLIES 9
Read only

former_member404244
Active Contributor
0 Likes
4,309

HI,

Try this

MODIFY ZTAB from table it_header.

If sy-subrc eq 0.

Commit work.

endif.

Regards,

Nagaraj

Read only

former_member188827
Active Contributor
0 Likes
4,309

first of all you dont need to use insert statement inside loop endloop.

secondly,

If the addition ACCEPTING DUPLICATE KEYS is specified, all rows are inserted for which this is possible. The remaining rows are rejected and sy-subrc is set to 4. The system field sy-dbcnt is set to the number of lines that are inserted. you must be passing values from internal table with same key as records of your ztable.that is shy system is inserting only one row.

Read only

Mohamed_Mukhtar
Active Contributor
0 Likes
4,311

Hi,


"Dont write this statment in LOOP....ENDLOOP
INSERT ZTAB from TABLE it_header[]  ACCEPTING DUPLICATE KEYS.
IF sy-subrc = 0.
MESSAGE 'Data Successfully saved into Database table' TYPE 'S'.
ENDIF.

Thanks

Read only

0 Likes
4,309

Hi,

I have use your code.

INSERT ZTAB from TABLE it_header[] ACCEPTING DUPLICATE KEYS.

IF sy-subrc = 0.

MESSAGE 'Data Successfully saved into Database table' TYPE 'S'.

ENDIF.

but it insert one record not multiple.

Thanks & Regards.

Sam.

Read only

0 Likes
4,309

Hi,

make sure that the records in the internal table it_header from where you are trying to insert do not contain data which has the key field values in the database table repeated. In that case 1 record will be added as the database has to main the database integrity of single record per key fields. Accepting duplicate keys prevents a runtime error, if removed you receive a dump that means you are trying to insert multiple records with the same key.

Hope this helps.

regards,

Sachin

Read only

Former Member
0 Likes
4,309

Hi,

You dont have to loop at it_header. You can directly use this


INSERT ZTAB FROM TABLE IT_HEADER ACCEPTING DUPLICATE KEYS.

Regards,

Vik

Read only

MarcinPciak
Active Contributor
0 Likes
4,309

accepting duplicate keys will prevent the program from dumping out. It will however not insert duplicating records as this violates DB restriction (only unique records can be stored in relational DB). But only one record (from two or three duplicating one) will be inserted in fact.

MODIFY (as suggested above) will handle the entires with same key in DB ( this is just insert+update statements in one ) but will also not solve it entirely, due to same reason.

Regards

Marcin

Read only

Former Member
0 Likes
4,309

Hi,

Try to use Work Area in MOVE-CORRESPONDING statement.

Hope it is helps.

Saurabh Goel

Read only

Former Member
0 Likes
4,309

Hi SAM,

Creating a SingleRecord -


>>>>

INSERT INSERT INTO <dbtab> VALUES <wa>.

Eg. DATAwa_spfliTYPEspfli.

...

wa_spfli-carrid = 'LH'.

wa_spfli-connid = '0007'.

wa_spfli-cityto = 'SINGAPORE'.

...

INSERT INTO spfli VALUES wa_spfli.

Fsy-subrc NE 0.

-


Creating Several Records---->>>>

INSERT <dbtab> FROM TABLE <itab>.

Eg.

DATA:it_spfli TYPE STANDARD TABLE OF spfli,

wa_itab LIKE LINE OF it_spfli.

...

wa_itab-carrid= 'LH'.

wa_itab-connid= '0009'.

wa_itab-cityto= 'HONGKONG'.

...

APPEND wa_itab TO it_spfli.

....

....

....

APPEND wa_itab TO it_spfli.

INSERT spfli FROM TABLE it_spfli.

IFsy-subrc NE 0.

....