Changing Line
The Open SQL statement for changing data in a database table is: UPDATE It allows you to change one or more lines in the database table
Specifying a Database Table
To specify the database table statically, enter the following for
UPDATE
where
To specify the database table dynamically, enter the following for
UPDATE (
where the field
You can use the CLIENT SPECIFIED addition to disable automatic client handling.
Changing Lines Column by Column
To change certain columns in the database table, use the following:
UPDATE
The WHERE clause determines the lines that are changed. If you do not specify a WHERE clause, all lines are changed. The expressions
i> =
The value in column i> is set to the value
i> =i> +
The value in column i> is increased by the value of
i> =i> -
The value in column i> is decreased by the value of
can be a data object or a column of the database table. You address the columns using their direct names.
If at least one line is changed, the system sets SY-SUBRC to 0, otherwise to 4. SY-DBCNT contains the number of lines changed.
If you use SET statements, you cannot specify the database table dynamically.
Overwriting Individual Lines From a Work Area
To overwrite a single line in a database table with the contents of a work area, use the following:
UPDATE FROM
The contents of the work area 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 contains 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.
A shortened form of the above statement is:
UPDATE
In this case, the contents of the table work area 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.
Overwriting Several Lines Using an Internal Table
To overwrite several lines in a database table with the contents of an internal table, use the following:
UPDATE FROM TABLE
The contents of the internal table
If the system cannot change a line because no line with the specified key exists, it does not terminate the entire operation, but continues processing the next line of the internal table.
If all lines from the internal table have been processed, SY-SUBRC is set to 0. Otherwise, it is set to 4. If not all lines are used, you can calculate the number of unused lines by subtracting the number of processed lines in SY-DBCNT from the total number of lines in the internal table. If the internal table is empty, SY-SUBRC and SY-DBCNT are set to 0.
Whenever you want to overwrite more than one line in a database table, it is more efficient to work with an internal table than to change the lines one by one.
Examples
UPDATE SFLIGHT SET PLANETYPE = 'A310'
PRICE = PRICE - '100.00'
WHERE CARRID = 'LH' AND CONNID = '0402'.
This example overwrites the contents of the PLANETYPE column with A310 and decreases the value of the PRICE column by 100 for each entry in SFLIGHT where CARRID contains ‘LH’ and CONNID contains ‘402’.
TABLES SPFLI.
DATA WA TYPE SPFLI.
MOVE 'AA' TO WA-CARRID.
MOVE '0064' TO WA-CONNID.
MOVE 'WASHINGTON' TO WA-CITYFROM.
...
UPDATE SPFLI FROM WA.
MOVE 'LH' TO SPFLI-CARRID.
MOVE '0017' TO SPFLI-CONNID.
MOVE 'BERLIN' TO SPFLI-CITYFROM.
...
UPDATE SPFLI.
CARRID and CONNID are the primary key fields of table SPFLI. All fields of those lines where the primary key fields are "AA" and "0064", or "LH" and "0017", are replaced by the values in the corresponding fields of the work area WA or the table work area SPFLI.
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.
...
UPDATE SPFLI FROM TABLE ITAB.
This example fills a hashed table ITAB and then overwrites the lines in SPFLI that have the same primary key (CARRID and CONNID) as a line in the internal table.
No comments:
Post a Comment