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
Specifying a Database Table
To specify the database table statically, enter the following for
INSERT INTO
where
To specify the database table dynamically, enter the following for
INSERT INTO (
where the field
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
The contents of the work area
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
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
Inserting Several Lines
To insert several lines into a database table, use the following:
INSERT
This writes all lines of the internal table
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
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.
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:
Post a Comment