UPDATE 

UPDATE

Variants:



1. UPDATE dbtab SET s1 ... sn.
2. UPDATE dbtab. or
UPDATE *dbtab. or
UPDATE (dbtabname) ... .
3. UPDATE dbtab FROM TABLE itab. or
UPDATE (dbtabname) FROM TABLE itab.

Effect

Updates values in a database table (see Relational Databases). You can specify the name of the database table either directly in the form dbtab or at runtime as contents of the field dbtabname. In both cases, the table must be known to the ABAP Dictionary. Normally, lines are updated only in the current client. Data can only be updated using a view if the view refers to a single table and was created in the ABAP Dictionary with the maintenance status "No restriction".

UPDATE belongs to the Open SQL command set.

Authorization checks (see


1. ... WHERE condition
2. ... CLIENT SPECIFIED

Effect

Updates values in a database table. If there is no WHERE clause , all lines (in the current client) are updated. If a WHERE condition is specified, only those records which satisfy the WHERE condition are updated.

The SET clause s1 ... sn identifies the columns to be updated and assigns values to them. Three types of SET statements si are supported:

f = g In all selected lines, the database table column determined by f receives the value of g. g can be an ABAP field, a literal, or a field descriptor . In the latter case, the contents of the column to which g points are copied into the line of the table that you are updating.

f = f + g In all selected lines, the contents of g are added to the value in the database table column determined by f. g can be an ABAP field, a literal, or a field descriptor. The NULL value remains unchanged. This statement can only be applied to a numeric field.

f = f - g In all selected lines, the content of g is subtracted from the value in the database table column determined by f . g can be an ABAP field, a literal, or a field descriptor. The NULL value remains unchanged. This statement can only be applied to a numeric field.

When the command has been executed, the system field SY-DBCNT contains the number of updated lines.

SY-SUBRC = 4:

No line was updated because either no line could be selected or the change would have generated lines with primary keys that already existed.

Note

With pooled and cluster tables, an UPDATE cannot change any primary key field.

Examples

Update discount for all customers (in the current client) to 3 percent:

TABLES SCUSTOM. 
 
UPDATE SCUSTOM SET DISCOUNT = '003'.

UPDATE SCUSTOM SET: DISCOUNT  = '003', 
                    TELEPHONE = '0621/444444' 
              WHERE ID        = '00017777'. 

defines statement chains,

Effect

Updates only those lines which satisfy the WHERE clause condition.

Example

Increase the number of occupied seats on Lufthansa flight 0400 on 28.02.1995 by 3 (in the current client):

TABLES SFLIGHT. 
 
UPDATE SFLIGHT SET   SEATSOCC = SEATSOCC + 3 
               WHERE CARRID   = 'LH'   AND 
                     CONNID   = '0400' AND 
                     FLDATE   = '19950228'.

Effect

Switches off automatic client handling. This allows you to update across all clients when using client-specific tables. The client field is treated like a normal table field, for which you can formulate suitable conditions in the WHERE clause.

The addition CLIENT SPECIFIED must immediately follow the database table name.

Example

Increase the number of occupied seats on Lufthansa flight 0400 on 28.02.1995 by 3 in client 2:

TABLES SFLIGHT. 
 
UPDATE SFLIGHT CLIENT SPECIFIED 
               SET   SEATSOCC = SEATSOCC + 3 
               WHERE MANDT    = '002'  AND 
               WHERE CARRID   = 'LH'   AND 
                     CONNID   = '0400' AND 
                     FLDATE   = '19950228'.

Variant 2

UPDATE dbtab. or
UPDATE *dbtab. or
UPDATE (dbtabname) ... .

1. ... FROM wa
2. ... CLIENT SPECIFIED

Effect

These are SAP-specific short forms that update one single line of a database table. The primary key for identifying the line to be updated and the values to be changed when specifying the database table name in the program are taken from the table work area, dbtab or *dbtab. If you specify the name of the database table directly, the program must also contain an appropriate TABLES statement. If the database table name is determined at runtime, you must use the addition FROM wa.

When the command has been executed, the system field SY-DBCNT contains the number of updated lines (0 or 1).

SY-SUBRC = 4:

No line was updated because no line with the specified primary key exists.

Examples

Update discount for the customer with the customer number '00017777' to 3 percent (in the current client):

TABLES SCUSTOM. 
 
SCUSTOM-ID       = '00017777'. 
SCUSTOM-DISCOUNT = '003'. 
UPDATE SCUSTOM.

Effect

Takes the values for the line to be updated not from the table work area dbtab, but from the explicitly specified work area wa. Here, the data is taken from wa, moving from left to right according to the structure of the table work area dbtab (defined with TABLES ). Since the structure of wa is ignored, the work area wa must be at least as wide (see DATA) as the table work area dbtab and the alignment of the work area wa must correspond to the alignment of the table work area. Otherwise, a runtime error occurs

Example

Update the telephone number of the customer with the customer number '12400177' in the current client:

TABLES SCUSTOM. 
DATA   WA LIKE SCUSTOM. 
 
WA-ID        = '12400177'. 
WA-TELEPHONE = '06201/44889'. 
UPDATE SCUSTOM FROM WA.

Note

If you do not explicitly specify a work area, the values for the line to be updated are taken from the table work area dbtab, even if the statement appears in a FORM or FUNCTION where the table work area is held in a formal parameter or a local variable.

Variant 3

UPDATE dbtab FROM TABLE itab. or
UPDATE (dbtabname) FROM TABLE itab.

... CLIENT SPECIFIED

Effect

Mass update of several lines in a database table. Here, the primary key for identifying the lines to be updated and the values to be changed are taken from the lines of the internal table itab . The lines of the internal table must satisfy the same conditions as the work area wa in addition 1 to variant 2.

The system field SY-DBCNT contains the number of updated lines, i.e. the number of lines in the internal table itab which have key values corresponding to lines in the database table.

SY-SUBRC = 0:

All lines from itab could be used to update the database table.

SY-SUBRC = 4:

At least one line of the internal table itab in the database table had no line with the same primary key. The other lines of the database table were updated.

Note

If the internal table itab is empty, SY-SUBRC and SY-DBCNT are set to 0.