Application Development 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: 

Insert Exception

Former Member
0 Kudos

Hi,

I m using Insert stmt to insert the records to ztable from itab.

insert ztable from table itab.

I m getting 'CX_SY_OPEN_SQL_DB' exception bcoz the record with the same key exists in the table. pls let me know how to catch this exception and how to set the message to the user

Thanks

5 REPLIES 5

Former Member
0 Kudos

Hmmmm... You should get a return code of 4, not a dump if you try to insert a record with a duplicate key.

In any event, use MODIFY instead of INSERT.

Rob

Former Member
0 Kudos

Hi,

You can read the table using a Select * using the table key before insterting the record so that if there is a record existing in the table then the sy-subrc will return as 0, then you can catch the exception or insert the record.

Another way is to use MODIFY instead of INSERT statement, this statement inserts the record if there is not record with that key or modifies the records if exists.

Regards,

Trikanth

Former Member
0 Kudos

I think it's something else. Can you post your code and dump analysis?

Rob

Former Member
0 Kudos

Hi,

The exception 'CX_SY_OPEN_SQL_DB' also occurs if your database table is empty.

Use the TRY..CATCH block to handle the exception (to avoid a dump).

e.g.: to handle a run-time error if the dbtab is empty..

TRY.

select * from ztable.

CATCH CX_SY_OPEN_SQL_DB.

ENDTRY.

insert ztable from table itab.

If the dump occurs even if the dbtab has data in it, then you should check to see if your itab contains rows with identical primary keys..

Regards,

Purnima.

Former Member
0 Kudos

Hi,

use the following to set the message to the user:

TRY.

insert ztable from table itab.

CATCH CX_SY_OPEN_SQL_DB.

message "Duplicate Insert due to same key" display like 'E'.

exit.

ENDTRY.

Shruthi