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 duplicate entries into table

Former Member
0 Likes
10,267

Hi experts,

I want to insert all records from internal table to database table (Dont bother about duplicates as i need all values from internal table) .

i used  "insert <dbtable> from <itab> accepting dupplicate keys". Then also it is not allowing to insert duplicates into DB. please advise me.

while defining itab  i used "non-unique default keys".

Thanks,

Silviya

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
6,027

Hi,

This is for sure that its not allowing you to enter duplicate records because of the primary key. If you want to enter all the records in any case, you have to change the primary key. And 1 tip i want to share. I always use update instead of insert. Its always safe.

Regards

Purnand

11 REPLIES 11
Read only

Former Member
0 Likes
6,027

Hello Silviya

You cannot enter duplicate records into Data base tables.Here duplicate in the sense you are trying enter one or more records, for which there is a similar record in the database exists with the same primary key.

Only thing you can do is go for UPDATE statement instead of INSERT.

Katrice

Read only

Former Member
0 Likes
6,027

Hi,

U cannot insert duplicate records since u hav a unique key field.

Still you want to insert, well remove the primary key of the DB table created and insert.

Otherwise u can only modify the table entries

Thanks,

Ben

Read only

Former Member
0 Likes
6,027

Hi Silviya,

    In your internal table check there is any duplicate records for your database table primery key. If it is you cant to insert else you can..

I hope this will help you.

Read only

vinoth_aruldass
Contributor
0 Likes
6,027

hi,

if the keys are specified in the table it won't accept the entry to come in ..  So if you can change the db as per your requirement if it is z table it is possible ..

hope it helps,

Vinoth

Read only

0 Likes
6,027

Hi Vinoth,

      My table is a customizing table only and also i cant get your answer clearly. If you can tell me clearlly please.

Thank YOu.

Read only

0 Likes
6,027

Hello John,

Refer following thread

http://scn.sap.com/thread/3224315

See SAP Documentation on INSERT

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.
  If, in this case,  the addition ACCEPTING DUPLICATE KEYS is not specified, no rows are inserted. 
Prior to Release 6.10, this raises an exception that cannot be handled; as of Release 6.10,
  this raises the exception that can be handled CX_SY_OPEN_SQL_DB.

Katrice

Read only

0 Likes
6,027

Hi Friend.

You can easily store duplicate entries in table using one column as SERIAL_NUMBER  part of primary key as follows

example is

SI.no          A          B        C     

   1           aaa       111      @@@

   2           aaa       111      @@@

   3           bbb       222      @@@

using  'INSERT ' keyword u can insert the data from DB table to internal table

Regards

Sujatha Sahu

Read only

Former Member
0 Likes
6,028

Hi,

This is for sure that its not allowing you to enter duplicate records because of the primary key. If you want to enter all the records in any case, you have to change the primary key. And 1 tip i want to share. I always use update instead of insert. Its always safe.

Regards

Purnand

Read only

RaymondGiuseppi
Active Contributor
0 Likes
6,027

"Dont bother about duplicates"

The primary key of the table definition are implemented in database via SQL, as a CONSTRAINT of UNIQUE KEY and NOT NULL,  and the database must prevent every tricky hint to be able to bypass this, even if you don't bother

So if you want to have multiple "duplicate entries" you MUST add another primary key, like an internal counter managed in your program. or use other type of database table (like INDX where you can EXPORT to database an internal table of data for one set of primary key)

Regards,

Raymond

Read only

Former Member
0 Likes
6,027

Make all the keys in the Table as Primary Keys . Duplicate entries will be allowed

Read only

Former Member
0 Likes
6,027

Hi Thomas,

For eg: Consider a database table ZEMPLOYEE and assume that this table has primary key field Employee_ID.

If you want to insert all the values from you internal table to database table irrespective of duplicate add field counter which will be the part of primary key field and insert.

Now primary key for this ZEMPLOYEE table will be EMP_ID and Counter fields.

Remember to increase the count number  to 1 for every new insertion.

loop at itab into wa.

     wa_counter = counter + 1.

     INSERT ZEMPLOYEE from wa.

endloop.


   Emp_ID    COUNTER       Emp_name       Emp_COUNTRY         

     1000                 1                           ABC                India

     1000                 2                          ABC                India