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 query

Former Member
0 Likes
1,491

Hi guys,

plz let me know if "UPDATE" statment works like "INSERT" at any point if there is no matching key fields in the "UPDATE" statement.

If yes. plz give me the exact syntax,

this is simple I guess.

Thx,

Ramesh

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,272

Hai Kpr,

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 database ). 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/4 Dictionary . If you specify the name of the database table directly, the program must also contain an appropriate TABLES statement. 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/4 Dictionary with the maintenance status "No restriction".

UPDATE belongs to the Open SQL command set.

Notes

Authorization checks 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 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 database system locking mechanism alone to synchronize simultaneous access to the same database by several users. Therefore, it is often necessary to use the SAP locking mechanism .

Variant 1

UPDATE dbtab SET s1 ... sn.

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 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 values of the ABAP/4 field or literal g .

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

f = f - g In all selected lines, the contents of the ABAP/4 field or literal g is subtracted from the value in the database table column determined by f . 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 value is set as follows:

SY-SUBRC = 0 At least one line was updated,

SY_SUBRC = 4 No line was updated because no line could be selected.

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

This addition 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

Effect

These are SAP-specific short forms which 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 the database table name is determined at runtime, you need to 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 value 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

Like 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 value 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

Like variant 1.

Regards

Sreeni

8 REPLIES 8
Read only

Former Member
0 Likes
1,273

Hai Kpr,

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 database ). 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/4 Dictionary . If you specify the name of the database table directly, the program must also contain an appropriate TABLES statement. 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/4 Dictionary with the maintenance status "No restriction".

UPDATE belongs to the Open SQL command set.

Notes

Authorization checks 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 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 database system locking mechanism alone to synchronize simultaneous access to the same database by several users. Therefore, it is often necessary to use the SAP locking mechanism .

Variant 1

UPDATE dbtab SET s1 ... sn.

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 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 values of the ABAP/4 field or literal g .

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

f = f - g In all selected lines, the contents of the ABAP/4 field or literal g is subtracted from the value in the database table column determined by f . 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 value is set as follows:

SY-SUBRC = 0 At least one line was updated,

SY_SUBRC = 4 No line was updated because no line could be selected.

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

This addition 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

Effect

These are SAP-specific short forms which 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 the database table name is determined at runtime, you need to 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 value 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

Like 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 value 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

Like variant 1.

Regards

Sreeni

Read only

Former Member
0 Likes
1,272

Hi KPR,

UPDATE only updates values in the database.

You might of refering to MOFIDY when you say if it ever inserts records.

Syntax for Update is:

1. UPDATE dbtab SET f1 ... fn. or

UPDATE (dbtabname) SET f1 ... fn.

2. UPDATE dbtab FROM wa. or

UPDATE (dbtabname) FROM wa.

3. UPDATE dbtab FROM TABLE itab. or

UPDATE (dbtabname) FROM TABLE itab.

4. UPDATE dbtab. or

UPDATE *dbtab.

and for MODIFY is:

1. MODIFY dbtab FROM wa. or

MODIFY (dbtabname) FROM wa.

2. MODIFY dbtab FROM TABLE itab. or

MODIFY (dbtabname) FROM TABLE itab.

3. MODIFY dbtab. or

MODIFY *dbtab.

4. MODIFY dbtab VERSION vers. or

MODIFY *dbtab VERSION vers.

Regards,

Vivek

Read only

Former Member
0 Likes
1,272

Hi Srini,

Thx for your response. I have read this document already. This "UPDATES" the Dbtab. that is fine.

My question is...Is update work like "INSERT" statement at any point?

Read only

0 Likes
1,272

Hi KPR,

No, update will not work like insert..MODIFY works like INSERT..

Regards,

Vivek

Read only

0 Likes
1,272

If I understood your question right, the answer is no. You can however use MODIFY which basically inserts a new record if the key fields are not found and if they are found updates with the new values.

hith

Sunil Achyut

Read only

Former Member
0 Likes
1,272

Thx Vivek.

But I have read in one book as "update" works like "insert" if there is no matching key fields...thats why I wanted to confirm if this would happen to use.

THANK YOU.

Read only

0 Likes
1,272

Hi KPR,

I don't think so...you can double check in the book.

Please award useful replies and close the thread if it is answered.

Regards,

Vivek

Read only

Former Member
0 Likes
1,272

Hi,

Update modifies record If exist or else

returns subrc as 4.

but modify statement creates new record

if record not present.

say ztest table has two fields

matnr werks

ztest-matnr = 'TEST'

ztest-werks = '1100'.

modify ztest.

Regards

Amole