Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

update syntax

Karan_Chopra_
Active Participant
0 Likes
40,504

plz tell me the update command syntax for updating a table using some where condition

4 REPLIES 4
Read only

Former Member
0 Likes
15,599

UPDATE sflight SET seatsocc = seatsocc + 3

WHERE carrid = 'LH' AND

connid = '0400' AND

fldate = '20010228'.

regards

shiba dutta

Read only

Former Member
0 Likes
15,599

Hi,

<b>UPDATE SFLIGHT SET PLANETYPE = 'A310' PRICE = PRICE - '100.00'

WHERE CARRID = 'LH' AND CONNID = '0402'</b>

Regards,

Padmam.

Read only

15,599

From SAP Help..

UPDATE

Variants:

1. UPDATE dbtab SET f1 ... fn.

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.

Notes

Authorization checks (see The SAP Authorization Concept) are not supported by the UPDATE statement. You must include these in the program yourself.

Changes to lines made with the UPDATE command only become final after a database commit (see Logical Unit of Work (LUW)). Prior to this, any database update can be canceled by a database rollback (see Programming Transactions).

In the dialog system, you cannot rely on the locking mechanism used by the database system (see Database Locking) to synchronize simultaneous access to the same database by several users. Therefore, it is often necessary to use SAP's locking mechanism (see SAP Locking).

You cannot change the primary key when you UPDATE a table for which a synchronous matchcode is defined.

Variant 1

UPDATE dbtab SET f1 ... fn.

Additions:

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 f1 ... fn identifies the columns to be updated and assigns values to them. Three types of SET statements fi 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.

The return code is set as follows:

SY-SUBRC = 0:

At least one line was updated,

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'.

Note

The 'colon and comma' logic in the program fragment

UPDATE SCUSTOM SET: DISCOUNT = '003',

TELEPHONE = '0621/444444'

WHERE ID = '00017777'.

defines statement chains,

not through a single statement which updates the discount and the telephone number of the customer with the customer number '00017777',

but by means of two statements where the first updates the discount for all customers and the second changes the telephone number of the customer with the customer number '00017777'.

Addition 1

... WHERE condition

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'.

Addition 2

... CLIENT SPECIFIED

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) ... .

Additions:

1. ... FROM wa

2. ... CLIENT SPECIFIED

In an ABAP Objects context, a more severe syntax check is performed that in other ABAP areas. See Short forms not allowed and * work areas not allowed.

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).

The return code is set as follows:

SY-SUBRC = 0:

The specified line was updated,

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.

Addition 1

... FROM wa

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.

Addition 2

... CLIENT SPECIFIED

Effect

As with variant 1.

Variant 3

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

Addition:

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

The return code is set as follows:

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.

Addition

... CLIENT SPECIFIED

Effect

As with variant 1.

Read only

Former Member
0 Likes
15,599

HI Karan Chopra,

Update command is used to Update a DB record ......../ records .......

If u want to update records which meets certain criteria then use the following command ...

<b>UPDATE</b> <DB Table Name> <b>SET</b> <Fieldname> = <value u want to pass>

<b>WHERE</b> <field 1> <b>=</b> <retrict value 1>

<b>AND</b> <field 2><b> =</b> <retrict value 2>

Objects in BOLD ==> keywords

Objects in < .. > ==> values which u want to pass / which u use ...

1. DB table name -> Updating DB table

2. Fieldname -> What record u want to update

3. value u want to pass -> the value u want to set/pass to <Fieldname>

4. field 1, field 2 .... -> The fields which uniquely identify the record which u want to update

4. retrict value 1, retrict value 2 --> The values with which u retrict for the Field 1 and Field 2 resp...

Hope this helps !

Cheers

Kripa Rangachari ...