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 from itab ACCEPTING DUPLICATE KEYS

ramesh_kajuru
Explorer
0 Likes
4,966

Hi,

I was wanting to know the behaviour of the ' INSERT from itab ACCEPTING DUPLICATE KEYS '.

My understanding is

If, for one or more of the rows to be inserted, a row with the same primary key or the same unique secondary key already exists and the ACCEPTING DUPLICATE KEYS addition is specified, the rows are not inserted and sy-subrc is set to 4.

My doubt is :

Is there a way where in i can insert the rows which are not existing in the database and then capture sy-subrc for further processing.

Or is there a better way of approching.

Thanks,

Ramesh

Hi,

I was wanting to know the behaviour of the ' INSERT from itab ACCEPTING DUPLICATE KEYS '.

My understanding is

If, for one or more of the rows to be inserted, a row with the same primary key or the same unique secondary key already exists and the ACCEPTING DUPLICATE KEYS addition is specified, the rows are not inserted and sy-subrc is set to 4.

My doubt is :

Is there a way where in i can insert the rows which are not existing in the database and then capture sy-subrc for further processing.

Or is there a better way of approching.

Thanks,

Ramesh

3 REPLIES 3
Read only

rahulkavuri
Active Contributor
0 Likes
2,714

hi in ur case modify statement solves the issue

modify statement works for inserting a record into the Database table if it doesnt exist and modifying an existing record if it already exists

modify ztable from itab

This is the syntax followed, reward points if found helpful

Read only

Former Member
0 Likes
2,714

Ramesh,

Could you please specify your exact requirement.

1) If you want to insert rows from internal table, 'ACCEPTING DUPLICATE KEYS' will prevent the program termination.

INSERT ZTEST_TABLE FROM TABLE LT_TABLE .

In case a duplicate row exist, you will get dump. So anytime you want to insert from internal table, you need to insert 'Accepting duplicate keys'.

2) If you are allowed to change existing rows, then you can use 'MODIFY' statement

3) If your requirement is to identify the individual rows that already exist (for reporting purpose), please use a SELECT statement with 'FOR ALL ENTRIES'.

If ZTABLE is your table with primary key k1.

SELECT K1 INTO TABLE ITAB2

FROM ZTABLE WHERE K1 = itab-k1 for all entries in itab.

ITAB2 will now contain all the entries that already exist in ITAB2.

Other option would be to loop at your original table and insert row by row, doing sy-subrc check to find duplicate rows.

LOOP AT ITAB1.

INSERT ZTABLE FROM ITAB1.

ENDLOOP.

Read only

Former Member
0 Likes
2,714

Hi

The Purpose of ACCEPTING DUPLICATE KEYS is as follows :

Mass insert: Inserts all lines of table itab in a single operation. The lines of itab must satisfy the same condition as the work area wa in variant 1.

After the statement has been executed, the system field SY-DBCNT contains the number of lines inserted.

If the system cannot insert a line, the program does not terminate with a runtime error. Instead, SY-SUBRC is set to 4. All lines of the internal table after the one that could not be inserted are still inserted into the database table.

Syntax

INSERT dbtab [CLIENT SPECIFIED] FROM TABLE itab. or INSERT (dbtabname) [CLIENT SPECIFIED] FROM TABLE itab.

Addition: ... ACCEPTING DUPLICATE KEYS

Regards

Naresh