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 with excisting key leads to sy-subrc 4 but not to exception CX_SY_OPEN_SQL_DB .

Former Member
0 Kudos
1,970

The SAP-Documentation informs about Exceptions in Open SQL: "Catchable Exceptions CX_SY_OPEN_SQL_DB [....]

Cause: You tried to insert a data record although a data record with the same key already exists on the database. Runtime Error: DBIF_RSQL_KEY_ALREADY_EXIST .

So I do not understand, why the Statement

INSERT <dbtab>

with an already excisting key at the database leads to sy-subrc 4 but not to the exception CX_SY_OPEN_SQL_DB :

in TRY...ENTRY this exception is not catched.

1 ACCEPTED SOLUTION

SuhaSaha
Advisor
Advisor
406

Hi Thorsten,

The behaviour depends on how you are trying to insert the record into the DB-Table.

  • If inserting from a structure(aka work-area) the SY-SUBRC will be set to 4
  • If inserting from an internal table without the addition ACCEPTING DUPLICATE KEY, then the exception CX_SY_OPEN_SQL_DB will be raised

BR,

Suhas

4 REPLIES 4

SuhaSaha
Advisor
Advisor
407

Hi Thorsten,

The behaviour depends on how you are trying to insert the record into the DB-Table.

  • If inserting from a structure(aka work-area) the SY-SUBRC will be set to 4
  • If inserting from an internal table without the addition ACCEPTING DUPLICATE KEY, then the exception CX_SY_OPEN_SQL_DB will be raised

BR,

Suhas

Former Member
0 Kudos
406

Thank you for your very helpful answers, Sandra Rossi and Suhas Saha

Sandra_Rossi
Active Contributor
0 Kudos
406

Read the documentation of INSERT carefully, the answer is in the "header part" (but I must admit that the doc would require a little reorganization as there's no mention of exception CX_SY_OPEN_SQL_DB here, especially we could deduce from sy-subrc = 4 that INSERT ... FROM TABLE ... would only lead to sy-subrc = 4 in case of duplicate primary key, although it would raise CX_SY_OPEN_SQL_DB if ACCEPTING DUPLICATE KEYS is not used @horst.keller ) :

System Fields

The statement INSERT sets the values of the system fields sy-subrc and sy-dbcnt.

sy-subrcMeaning0When a work area was declared in source, the specified row was changed. In the declaration of an internal table in source, all declared rows were inserted, or the internal table is empty.2When a LOB handle structure was specified with a component for writer streams, the non-LOB handle components were not yet written to the database, but instead are passed when the stream is closed, at the latest. Whether this situation occurs or not depends on the database. Refer to LOB handles.4When a work area was specified in source, the declared row was not inserted, or, when an internal table was specified in source, not all declared rows were inserted, since a row in the database table has the same primary key or a unique secondary index.

Former Member
0 Kudos
406

Thank you for your very helpful answers, Sandra Rossi and Suhas Saha