ABAP Keyword Documentation →  ABAP − Reference →  Processing External Data →  ABAP Database Accesses →  Open SQL →  Open SQL - Write Accesses →  INSERT dbtab → 

INSERT dbtab - source

Short Reference

Syntax

... wa
  | {TABLE itab [ACCEPTING DUPLICATE KEYS] }.

Alternatives:

1. ... wa ...

2. ... TABLE itab [ACCEPTING DUPLICATE KEYS] ...

Effect

You can specify a non-table-like data object wa after FROM and VALUES. After FROM, you can also specify an internal table itab. The contents of the row(s) to be inserted are taken from these data objects.

Alternative 1

... wa ...


Effect

After VALUES and FROM you can specify a non-table-like work area wa, from whose content a row is created for insertion in the database table. The work area must fulfill the prerequisites for use in Open SQL statements.

The new row is inserted in the database table if this does not already contain a row with the same primary key or the same unique secondary index. If it does, the row is not inserted and sy-subrc is set to 4.

If a view is specified in target that does not include all columns in the database table, these are set to the type-related initial value or to the null value in the inserted rows. The latter applies only if, for the relevant database column, the attribute initial value is not selected in ABAP Dictionary.

By default, an automatic client handling is performed, which means that a client identifier specified in wa is not considered, but the current client is used instead. This has no effect on wa. You can switch off automatic client handling using the addition CLIENT SPECIFIED.

Notes

Example

Inserting a new airline in the database table SCARR.

DATA scarr_wa TYPE scarr.

scarr_wa-carrid   = 'FF'.
scarr_wa-carrname = 'Funny Flyers'.
scarr_wa-currcode = 'EUR'.
scarr_wa-url      = 'http://www.funnyfly.com'.

INSERT INTO scarr VALUES scarr_wa.

Alternative 2

... TABLE itab [ACCEPTING DUPLICATE KEYS] ...


Effect

You can specify an internal table itab after FROM, from whose content multiple rows are created for insertion in the database table. The row type of the internal table must fulfill the prerequisites for use in Open SQLstatements.

The content of each row of the internal table is composed using the same rules as for a single work area wa with the exception that when inserting from an internal table locators operate as the source but no write streamscan be created.

If no row with the same primary key or with the same unique secondary index exists in the database table for any of the rows to be inserted, all rows are inserted and sy-subrc is set to 0. If the internal table is empty, no rows are inserted. However sy-subrc is still set to 0. The system field sy-dbcnt is set to the number of rows that are inserted.

If a row with the same primary key or the same unique secondary index exists in the database table for one or more of the rows to be inserted, these rows cannot be inserted. In this situation, there are three possibilities:

  1. Using ACCEPTING DUPLICATE KEYS

    If the addition ACCEPTING DUPLICATE KEYS is specified, all rows are inserted for which this is possible. The remaining rows are discarded and sy-subrc is set to 4. The system field sy-dbcnt is set to the number of rows that are inserted.

  2. Handling an exception

    If the addition ACCEPTING DUPLICATE KEYS is not specified, the handleable exception CX_SY_OPEN_SQL_DB is raised. Rows continue to be inserted until the exception is raised. The number of inserted rows is undefined. The system fields sy-subrc and sy-dbcnt retain their previous value.

  3. Runtime Errors

    If the addition ACCEPTING DUPLICATE KEYS is not specified and if the exception is not handled, then a runtime error occurs. This executes a database rollback that rolls back all changes to the current database LUW. This applies in particular to rows that were inserted before a double entry occurred.

Notes