Entering content frame

Inserting Lines into Tables Locate the document in its SAP Library structure

The Open SQL statement for inserting data into a database table is:

INSERT INTO target lines.

It allows you to insert one or more lines into the database table target. You may specify the database table targeteither statically or dynamically.

Specifying a Database Table

To specify the database table statically, enter the following for target:

INSERT INTO dbtab [CLIENT SPECIFIED] lines.

where dbtab is the name of a database table defined in the ABAP Dictionary.

To specify the database table dynamically, enter the following for target:

INSERT INTO (name) [CLIENT SPECIFIED] lines.

where the field name contains the name of a database table defined in the ABAP Dictionary.

You can use the CLIENT SPECIFIED addition to disable automatic client handling.

Inserting a Single Line

To insert a single line into a database table, use the following:

INSERT INTO target VALUES wa.

The contents of the work area wa are written to the database table dbtab. It is a good idea to define the work area with reference to the structure of the database table.

You can also insert single lines using the following shortened form of the INSERT statement:

INSERT target FROM wa.

Using FROM instead of VALUE allows you to omit the INTOclause. Shorter still is:

INSERT dbtab.

In this case, the contents of the table work area dbtab are inserted into the database table with the same name. You must declare this table work area using the TABLES statement. In this case, it is not possible to specify the name of the database table dynamically. Table work areas with the same name as the database table (necessary before Release 4.0) should no longer be used for the sake of clarity.

Inserting Several Lines

To insert several lines into a database table, use the following:

INSERT target FROM TABLE itab [ACCEPTING DUPLICATE KEYS].

This writes all lines of the internal table itabto the database table in one single operation. If one or more lines cannot be inserted because the database already contains a line with the same primary key, a runtime error occurs. You can prevent the runtime error occurring by using the addition ACCEPTING DUPLICATE KEYS.

Whenever you want to insert more than one line into a database table, it is more efficient to work with an internal table than to insert the lines one by one.

Examples

Example

Adding single lines

TABLES spfli.

DATA wa TYPE spfli.

wa-carrid = 'LH'.
wa-cityfrom = 'WASHINGTON'.
...
INSERT INTO spfli VALUES wa.

wa-carrid = 'UA'.
wa-cityfrom = 'LONDON'.
...
INSERT spfli FROM wa.

spfli-carrid = 'LH'.
spfli-cityfrom = 'BERLIN'.
...
INSERT spfli.

This program inserts a single line into the database table SPFLI using each of the three possible variants of the INSERTstatement.

Instead of

INSERT spfli.

in the last line, you could also use the longer forms

INSERT spfli FROM spfli

or

INSERT INTO spfli VALUES spfli

here. The name SPFLI is therefore not unique.

These variations of the INSERT addition only work with table work areas that have been declared using TABLESand should therefore no longer be used.

Example

DATA: itab TYPE HASHED TABLE OF spfli
                WITH UNIQUE KEY carrid connid,
      wa LIKE LINE OF itab.

wa-carrid = 'UA'. wa-connid = '0011'. wa-cityfrom = ...
INSERT wa INTO TABLE itab.

wa-carrid = 'LH'. wa-connid = '1245'. wa-cityfrom = ...
INSERT wa INTO TABLE itab.

wa-carrid = 'AA'. wa-connid = '4574'. wa-cityfrom = ...
INSERT wa INTO TABLE itab.

...

INSERT spfli FROM TABLE itab ACCEPTING DUPLICATE KEYS.

IF sy-subrc = 0.
  ...
ELSEIF sy-subrc = 4.
  ...
ENDIF.

This example fills a hashed table itaband inserts its contents into the database table SPFLI. The program examines the contents of sy-subrc to see if the operation was successful.

 

 

Leaving content frame