Wednesday, June 10, 2009

Inserting Lines into Tables

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

INSERT INTO .

It allows you to insert one or more lines into the database table . You can only insert lines into an ABAP Dictionary view if it only contains fields from one table, and its maintenance status is defined as Read and change. You may specify the database table either statically or dynamically.

Specifying a Database Table

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

INSERT INTO [CLIENT SPECIFIED] .

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

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

INSERT INTO () [CLIENT SPECIFIED] .

where the field 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 VALUES .

The contents of the work area are written to the database table . The work area must be a data object with at least the same length and alignment as the line structure of the database table. The data is placed in the database table according to the line structure of the table, and regardless of the structure of the work area. It is a good idea to define the work area with reference to the structure of the database table.

If the database table does not already contain a line with the same primary key as specified in the work area, the operation is completed successfully and SY-SUBRC is set to 0. Otherwise, the line is not inserted, and SY-SUBRC is set to 4.

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

INSERT FROM >.

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

INSERT .

In this case, the contents of the table work area 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 FROM TABLE [ACCEPTING DUPLICATE KEYS] .

This writes all lines of the internal table to the database table in one single operation. The same rules apply to the line type of as to the work area described above.

If the system is able to insert all of the lines from the internal table, SY-SUBRC is set to 0. 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. In this case, the lines that would otherwise cause runtime errors are discarded, and SY-SUBRC is set to 4.

The system field SY-DBCNT contains the number of lines inserted into the database table, regardless of the value in SY-SUBRC.

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 INSERT statement.

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

The name SPFLI is therefore not unique.

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 ITAB and inserts its contents into the database table SPFLI. The program examines the contents of SY-SUBRC to see if the operation was successful.

No comments:

Blog Archive