MODIFY (ABAP Keyword)


MODIFY is a keyword used in SAP ABAP programming.
This tutorial covers its introduction & syntax details.

MODIFY

MODIFY – Change a database table

Variants

1. MODIFY dbtab. or
MODIFY *dbtab. or
MODIFY (dbtabname) … . .
2. MODIFY dbtab FROM TABLE itab. or
MODIFY (dbtabname) FROM TABLE itab.
3. MODIFY dbtab VERSION vers. or
MODIFY *dbtab VERSION vers.

Effect
Inserts
new lines or updates existing lines in a database table . If a line
with the specified primary key already exists, an INSERT is executed.
Otherwise, an UPDATE is performed. You can specify the name of the
database table either in the program itself in the form MODIFY dbtab
… or at runtime as the contents of the field dbtabname in the form
MODIFY (dbtabname) … . In both cases, the database table must be
defined in the ABAP/4 Dictionary. If the program contains the name of
the database table, it must also have a corresponding TABLES statement.
Normally, records are inserted or updated only in the current client.
Data can only be inserted or 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”.

MODIFY belongs to the Open SQL command set.

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

The return code value is set as follows:

SY-SUBRC = 0 All lines were successfully inserted or updated. Any other result causes a runtime error.

Notes
Automatic
definition of INSERT and UPDATE is expensive. You should therefore use
MODIFY only if you cannot define the INSERT and UPDATE cases yourself
in the program.
Since the MODIFY statement does not perform authority checks , you have to program them yourself.

Variant 1
MODIFY dbtab. or
MODIFY *dbtab. or
MODIFY (dbtabname) … .

Additions

1. … FROM wa
2. … CLIENT SPECIFIED

Effect
Inserts
a new line or updates an existing line in a database table. If you
specify the name of the database table yourself, the primary key for
identifying the line to be inserted or updated and the relevant values
are taken from the table work area dbtab or *dbtab (see TABLES ). If
the name of the database table is not determined until runtime, you
need to use the addition … FROM wa .

Example
Insert or change data of the customer Robinson in the current client:

TABLES SCUSTOM.
SCUSTOM-ID = ‘12400177’.
SCUSTOM-NAME = ‘Robinson’.
SCUSTOM-POSTCODE = ‘69542’.
SCUSTOM-CITY = ‘Heidelberg’.
SCUSTOM-CUSTTYPE = ‘P’.
SCUSTOM-DISCOUNT = ‘003’.
SCUSTOM-TELEPHONE = ‘06201/44889′.

MODIFY SCUSTOM.

Addition 1
… FROM wa

Effect
The
values for the line to be inserted or upodated are not taken from the
table work area dbtab , but from the explicitly specified work area wa
. When doing this, the data is read from left to right according to the
structure of the table work area dbtab (see TABLES ). Since the
structure of wa is not taken into account, 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.

Note
If a
work area is not explicitly specified, the values for the line to be
inserted or updated are also taken from the table work area dbtab if
the statement is in a FORM or FUNCTION where the table work area is
storeed in a formal parameter or local variable of the same name.

Addition 2
… CLIENT SPECIFIED

Effect
Switches
off automatic client handling. This allows you to edit data across all
clients even when dealing with client-specific tables. The client field
is treated like a normal table field that can be programmed to accept
values in the table work area dbtab or *dbtab where the line to be
edited occurs.

The addition CLIENT SPECIFIED must be specified immediately after the name of the database table.

Variant 2
MODIFY dbtab FROM TABLE itab. or
MODIFY (dbtabname) FROM TABLE itab.

Addition

… CLIENT SPECIFIED

Effect
Mass
modify: Inserts new lines or updates existing lines of a database
table. The primary keys for identifying the lines to be inserted or
updated and the relevant values are taken from the internal table itab
. The lines of the internal table itab must satisfy the same conditions
as the work area wa in addition 1 to variant 1.

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

Addition
… CLIENT SPECIFIED

Effect
As for variant 1.

Variant 3
MODIFY dbtab VERSION vers. or
MODIFY *dbtab VERSION vers.

Note
This variant is obsolete .

Effect
Inserts
a new line or updates an existing line in a database table, the name of
which is taken from the field vers at runtime. If no line exists with
the specified primary key, an INSERT is executed. Otherwise, an UPDATE
is performed. The database table must be defined in the ABAP/4
Dictionary and its name must conform to the naming conventions for R/2
ATAB tables. These stipulate that the name must begin with ‘T’ and may
contain up to four further characters. The field vers must contain the
table name without the leading ‘T’. Only lines in the current client
are inserted or updated. The line to be inserted is taken from the
statically specified table work area dbtab or *dbtab .

SY-SUBRC
is set to 0 if the line is successfully inserted or updated. SY-SUBRC
<> 0 is not possible since any other result causes a runtime
error.

MODIFY – Change an internal table

Variant
MODIFY itab [FROM wa] [INDEX idx].

Effect
Changes an entry in the internal table itab .

If
you specify FROM wa , the line is replaced by the explicitly specified
work area wa . If the FROM specification is omitted, the line is
replaced by the header line from itab .

With INDEX idx , you can
specify the table index of the line to be changed. The index
specification can be omitted in a LOOP on an internal table.

The INDEX specification can also appear before the FROM specification.

The return code value is set as follows:

When specifying the insertion point with INDEX idx :

SY-SUBRC = 0 The change was executed.
SY_SUBRC = 4 The index specification was too big. The change was not executed because the table had fewer than idx entries.
If you do not specify the insertion point, the &ABAP_SUBRC is set to 0.

Note
The counting of table entries begins with 1.

Note
Performance

You
can avoid unnecessary assignments by using statements which have an
explicitly specified work area for internal tables with a header.

The runtime required to execute the MODIFY itab INDEX idx statement is about 5 msn (standardized microseconds).

MODIFY – Change a list line

Variants

1. MODIFY LINE n.
2. MODIFY LINE n OF CURRENT PAGE.
3. MODIFY LINE n OF PAGE m.
4. MODIFY CURRENT LINE.

Variant 1
MODIFY LINE n.

Additions

1. … INDEX idx
2. … LINE FORMAT fmt1 … fmtn
3. … FIELD VALUE f1 FROM g1 … fn FROM gn
4. … FIELD FORMAT f1 fmt11 … fmt1m … fn fmtn1 … fmtnm

Effect
Changes
the n th line of the list. This could be, for example, after line
selection ( AT LINE-SELECTION , AT PFxx , AT USER-COMMAND ).
The
current contents of the system field SY-LISEL are restored to the list
as the line contents and the HIDE area for the line is re-determined
from the current contents of the fields hidden with HIDE .

The return code value is set as follows:

SY-SUBRC = 0 Line was successfully changed.
SY-SUBRC <> 0 Line does not exist.

Note
With
multiple-level line selection, the modification is always performed in
the list where the (last) line selection was made, except in the case
of the addition … INDEX idx and MODIFY CURRENT LINE (see below).

Addition 1
… INDEX idx

Effect
Changes the relevant line in the list to list level idx (0, 1, 2, …) with multiple line selection ( SY-LSIND ).

Addition 2
… LINE FORMAT fmt1 … fmtn

Effect
The
output format of the selected line is determined by the format
specifications fmt1 , fmt2 … . For a list of valid format
specifications, see FORMAT .

Example

DATA I TYPE I VALUE 2.

WRITE: / ‘Intensified’ INTENSIFIED,
‘Input’ INPUT,
‘color 1′ COLOR 1,
‘intensified off’ INTENSIFIED OFF.

* Line selection
AT LINE-SELECTION.
MODIFY CURRENT LINE
LINE FORMAT INVERSE
INPUT OFF
COLOR = I.

After
you have selected the the output list line (by double-clicking), the
whole line is set to COLOR 2 and INVERSE and all INPUT fields are set
to INPUT OFF . The fields with the attribute INTENSIFIED or INTENSIFIED
OFF retain this because the attribute is not addressed here.

Addition 3
… FIELD VALUE f1 FROM g1 … fn FROM gn

Effect
Overwrites
the contents of the fields f1 , f2 , … in the list line with the
current contents of the fields g1 , g2 , … (type conversion as for
MOVE g1 , g2 , … to type C). The field contents of f1 , f2 , …
themselves remain unchanged.

Notes
If a field (e.g. f2 ) is
output several times in the line to be modified, only the first
occurrence is modified. If the field is not output in the line at all,
it is ignored.
You can omit the addition FROM g2 if the field f2 in the list line is to be modified from the current contents of f2 .

This means that

… FIELD VALUE f2

has the same effect as

… FIELD VALUE f2 FROM f2

The
return code value of SY-SUBRC is not affected by the addition FIELD
VALUE and so only depends on the existence of the selected list line.

Addition 4
… FIELD FORMAT f1 fmt11 … fmt1m
… fn fmtn1 … fmtnm

Effect
Modifies the output format of the field f1 according to the format specifications fmt11 … fmt1m .
Similar
to f2 , …, fn . For a list of valid format specifications, see FORMAT
. Fields that occur several times or not at all in the line are treated
as in the addition FIELD VALUE .

Notes
If you combine the
additions LINE FORMAT and FIELD FORMAT , the format set by LINE FORMAT
is always valid for the whole line initially. Afterwards, it is changed
by the format specifications for the individual fields.

Example

DATA: FLAG VALUE ‘X’,
TEXT(20) VALUE ‘Australia’,
I TYPE I VALUE 7.
FORMAT INTENSIFIED OFF.
WRITE: / FLAG AS CHECKBOX, TEXT COLOR COL_NEGATIVE.

AT LINE-SELECTION.
MODIFY CURRENT LINE
LINE FORMAT INTENSIFIED
FIELD VALUE FLAG FROM SPACE
FIELD FORMAT FLAG INPUT OFF
TEXT COLOR = I.

When
the user selects the displayed list line by double-clicking, the
checkbox for FLAG is reset and can no longer accept values. The format
of the entire line is set to “intensified” and TEXT is displayed in a
different color.

Variant 2
MODIFY LINE n OF CURRENT PAGE.

Additions

1. … FIELD VALUE f1 FROM g1 … fn FROM gn
2. … LINE FORMAT fmt1 .. fmtn
3. … FIELD FORMAT f1 fmt11 … fmt1m … fn fmtn1 … fmtnm

Effect
Changes the n th line on the current page (stored in the system field SY-CPAGE ).

Addition 1
… FIELD VALUE f1 FROM g1 … fn FROM gn
Addition 2
… LINE FORMAT fmt1 .. fmtn
Addition 3
… FIELD FORMAT f1 fmt11 … fmt1m
… fn fmtn1 … fmtnm

Effect
See MODIFY LINE

Variant 3
MODIFY LINE n OF PAGE m.

Additions

1. … FIELD VALUE f1 FROM g1 … fn FROM gn
2. … LINE FORMAT fmt1 … fmtn
3. … FIELD FORMAT f1 fmt11 … fmt1m … fn fmtn1 … fmtnm

Effect
Changes the n th line on page m .

Addition 1
… FIELD VALUE f1 FROM g1 … fn FROM gn
Addition 2
… LINE FORMAT fmt1 … fmtn
Addition 3
… FIELD FORMAT f1 fmt11 … fmt1m
… fn fmtn1 … fmtnm

Effect
See MODIFY LINE

Variant 4
MODIFY CURRENT LINE.

Additions

1. … FIELD VALUE f1 FROM g1 … fn FROM gn
2. … LINE FORMAT fmt1 … fmtn
3. … FIELD FORMAT f1 fmt11 … fmt1m … fn fmtn1 … fmtnm

Effect
Changes
the last line read (with line selection or READ LINE ), even across
line levels. This variant is especially useful if the line to be
modified has been read immediately before through line selection or
using READ LINE . You then need to note the number of the line until
the MODIFY .

Addition 1
… FIELD VALUE f1 FROM g1 … fn FROM gn
Addition 2
… LINE FORMAT fmt1 … fmtn
Addition 3
… FIELD FORMAT f1 fmt11 … fmt1m
… fn fmtn1 … fmtnm

Effect
See MODIFY LINE

MODIFY – Change the attributes of a screen field

Basic form
MODIFY SCREEN.

Effect
Changes the attributes belonging to the current screen field whilst processing with LOOP AT SCREEN … ENDLOOP .
The
attributes of all fields of a screen are stored in the system table
SCREEN . This can be edited line by line using LOOP AT SCREEN …
ENDLOOP . Changes to the properties of the attributes of the current
screen field (= current line in the system table SCREEN ) can be put
into effect using MODIFY SCREEN .

Note
This statement should
be used only within a LOOP AT SCREEN … ENDLOOP loop at PBO time as
part of the process logic of a screen.