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

Inserting duplicates into table

Former Member
0 Likes
2,923

Hi everyone,

I'm new to ABAP and need help.

Below line of code inserts data from internal table ITEVEN into data dictionary table ZTEVDC.

-----------------------------------------------------------------------

INSERT ZTEVDC FROM TABLE ITEVEN.

-----------------------------------------------------------------------

However, sometimes there can be data in ITEVEN, that already exist in ZTEVDC with same primary key.

I want the code to save records from ITEVEN into ZTEVDC, however, if record already exist in ZTEVDC(With primary key PDSNR) then it should modify the existing record and not create a duplicate(Which crashes the program). If record does not exist in ZTEVDC, then a new record will be created.

Currently the system crashes if a record with same primary key(PDSNR) already exist in ZTEVDC.

Can anyone please help me?

BR

Turan

8 REPLIES 8
Read only

vigneshyeram
Active Participant
0 Likes
2,496

Dear Egemen,

Please try this.

Modify ztevdc from table iteven.

commit work.

Hope it helps you.

Thanks & Regards,

Vignesh Yeram

Read only

Former Member
0 Likes
2,496

Hi Egemen,

Use MODIFY statement.

For your information.

Modify statement inserts a new records if there is no record exist with the same key combination fields and updates the table if record already exists in database table.

While INSERT statement only inserts a new record and gives short dump in case of duplicate entry.

Regards,

Amit Kumar Singh

Read only

Former Member
0 Likes
2,496

Hi,

in this case you can use MODIFY statement.

An example:

* Updates database table Ztable with the contents of wa_itab

MODIFY Ztable FROM wa_itab.

other example

* Updates database table Ztable with the contents of it_itab

MODIFY Ztable FROM TABLE it_itab.

Regards

Ivan

Read only

Former Member
0 Likes
2,496

Egemen,

Like stated above the modify statement will work. I just want to also point out if you do want to use insert but avoid duplicate entries, this will work.

insert ztevdc from table iteven accepting duplicate keys.

if sy-subrc <> 0.

     "your error message

endif.

This will prevent the run-time error occurring and set sy-subrc to 4 while disregarding the data you tried to insert.

Thanks,

Justin

Read only

Former Member
0 Likes
2,496

Hi,

Instead of INSERT statement, use MODIFY statement which is the best option to proceed with in your scenario.

Regards,

Ravi.

Read only

uppu_narayan
Active Participant
0 Likes
2,496

Modify statement is the solution for your question...................

Read only

0 Likes
2,496

This message was moderated.

Read only

0 Likes
2,496

Hi Egemen,

In your case use MODIFY instead of INSERT.

basic difference between INSERT, UPDATE & MODIFY --

INSERT: when new record has to inserted into table.

UPDATE: When record in present in table & need to be change.

MODIFY: this combination of both Insert & Update, means if record is not present it will add the record into table otherwise it will update the existing record without inserting again.

For more information check these link.

Link1

Link2

Regards,

Jyoti