‎2007 Jan 03 5:18 PM
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.
Thanks,
Ramesh
‎2007 Jan 03 7:18 PM
Hi ramesh,
Here is behaviour of Use of ACCEPTING DUPLICATE KEYS :
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 can do one thing if you want to insert row which are not already existing.
Declare one internal table. use select single statement to fetch the same record from database based on primary or secondary key(you are using for insert based on internal table). If sy-subrc not equal to zero then row is not existing. then you can insert the record.
Ashven
‎2007 Jan 03 7:18 PM
Hi ramesh,
Here is behaviour of Use of ACCEPTING DUPLICATE KEYS :
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 can do one thing if you want to insert row which are not already existing.
Declare one internal table. use select single statement to fetch the same record from database based on primary or secondary key(you are using for insert based on internal table). If sy-subrc not equal to zero then row is not existing. then you can insert the record.
Ashven
‎2007 Jan 03 7:18 PM
Hello Ramesh,
The Insert statement will not insert Duplicate keys in a table and when you specify the same key and try to update or insert an entry in the table it always fails..if u want to insert the duplicate values in the table then alter the key combination in the table.
For your doubt try to Select the entries from the table with the given key combination and if sy-subrc fails i.e. if it is 4 then Insert it else do not.
Srini.
‎2007 Jan 04 1:11 PM
Hi Ramesh,
You can use the command INSERT <dbtab> FROM TABLE <itab> to create several rows in a database table.
The internal table <itab> that you should specify here must have the same line structure as the corresponding database table and the new data records. The client field that might exist in the internal table <itab> will only be taken into consideration if the CLIENT SPECIFIED addition is specified. If there is no CLIENT SPECIFIED addition, the current execution client applies. If it is possible to create all the lines, sy-subrc is automatically set to zero.<i><b>If, however, even one data record cannot be created, a runtime error occurs. This means that the entire insertion operation is discarded (database rollback). If, in such a case, you want to have records that can be inserted actually inserted, use the command addition ACCEPTING DUPLICATE KEYS. If there is an error, this addition suppresses the runtime error (and thus also the database rollback), sets the return code (sy-subrc) to 4, and inserts all the records without errors</b></i>.The sy-dbcnt system field contains the number of rows that were successfully inserted in the database.
Regards,
Balaji Reddy G
***Rewards if answers are helpful
‎2007 Jan 04 1:15 PM
Hi ramesh,
Your understanding is perfectly right.
While we are using in OOPS we are going to check sy-subrc value to trace status of SQL Statement.
Please use Select single query and find out whether record exist or not in DB and proceed further processing.
Regards
Bhupal Reddy