Changing Lines in ABAP Internal Tables

MODIFY is the statement to change single or multiple lines in an internal table. Use the INDEX addition to change a single line. If we use the INDEX addition and the operation is successful, SY-SUBRC will be set to zero and the contents of the work area overwrites the contents of the line with the corresponding index.

Instead of changing all the values of a row we can specify the fields we want to change by specifying the fieldnames in the TRANSPORTING addition.

MODIFY <internal table> FROM <work area> [INDEX <index>] 
                              [TRANSPORTING <f1> <f2> ... ].

We can also use the above MODIFY statement without INDEX addition inside LOOP. Inside LOOP if we do not specify the INDEX, then the current loop line will be modified.

We can use the WHERE clause to change single or multiple lines. All the lines that meet the logical condition will be processed. If at least one line is changed, the system sets SY-SUBRC to 0, otherwise to 4.

MODIFY <internal table> FROM <work area> 
          TRANSPORTING <f1> <f2> ... WHERE <condition>.
*--------------------------------------------------------------*
*Data Types
*--------------------------------------------------------------*
TYPES: BEGIN OF ty_student,
       id(5)     TYPE n,
       name(10)  TYPE c,
       place(10) TYPE c,
       age       TYPE i,
       END OF ty_student.

*--------------------------------------------------------------*
*Data Declaration
*--------------------------------------------------------------*
DATA: gwa_student TYPE ty_student.
DATA: it  TYPE TABLE OF ty_student.

gwa_student-id     = 1.
gwa_student-name   = 'JOHN'.
gwa_student-place  = 'London'.
gwa_student-age    = 20.
INSERT gwa_student INTO TABLE it.

gwa_student-id     = 2.
gwa_student-name   = 'JIM'.
gwa_student-place  = 'New York'.
gwa_student-age    = 21.
INSERT gwa_student INTO TABLE it.

gwa_student-id     = 3.
gwa_student-name   = 'JACK'.
gwa_student-place  = 'Bangalore'.
gwa_student-age    = 20.
INSERT gwa_student INTO TABLE it.

gwa_student-id     = 4.
gwa_student-name   = 'ROB'.
gwa_student-place  = 'Bangalore'.
gwa_student-age    = 22.
INSERT gwa_student INTO TABLE it.

WRITE:/ 'Values in IT before MODIFY' COLOR 4.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5,
        37 'Age' COLOR 5.
LOOP AT it INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place,
          gwa_student-age.
ENDLOOP.

SKIP.
WRITE:/ 'Values in IT after MODIFY' COLOR 4.

gwa_student-id     = 4.
gwa_student-name   = 'ROB'.
gwa_student-place  = 'Mumbai'.
gwa_student-age    = 25.

*Change all the columns of row 4 with work area values
MODIFY it FROM gwa_student INDEX 4.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5,
        37 'Age' COLOR 5.
LOOP AT it INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place,
          gwa_student-age.
ENDLOOP.

SKIP.
WRITE:/ 'Values in IT after Transporting addition' COLOR 4.

gwa_student-id     = 9.
gwa_student-name   = 'TOM'.
gwa_student-place  = 'Bangalore'.
gwa_student-age    = 30.
*Change specific columns of row 4 with work area values by
*using TRANSPORTING addition
MODIFY it FROM gwa_student INDEX 4 TRANSPORTING place.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5,
        37 'Age' COLOR 5.
LOOP AT it INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place,
          gwa_student-age.
ENDLOOP.

SKIP.
WRITE:/ 'Values in IT after MODIFY using WHERE Clause' COLOR 4.

gwa_student-place  = 'Mumbai'.

*Change multiple rows using WHERE clause
MODIFY it FROM gwa_student TRANSPORTING place
                           WHERE place = 'Bangalore'.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5,
        37 'Age' COLOR 5.
LOOP AT it INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place,
          gwa_student-age.
ENDLOOP.

Output

modify-it-1


1 Comment

Comments are closed.