Skip to Content

INSERT with excisting key leads to sy-subrc 4 but not to exception CX_SY_OPEN_SQL_DB .

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.

Add comment
10|10000 characters needed characters exceeded

  • 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.

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

  • Get RSS Feed

1 Answer

  • Best Answer
    Oct 24, 2016 at 03:01 PM

    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

    Add comment
    10|10000 characters needed characters exceeded