Adding Data to an Internal Table

To add a single row to an internal table, you can use the append statement. Append copies a single row from any work area and places it in the body at the end of the existing rows. The work are can be the header line, or it can be any other field string having the same structure as a row in the body.

Syntax for append statement

append <wa> to <internal table (body)>.

where:
  • wa is the name of the work area.
The following points apply:
  • wa must have the same structure as a row of the body.
  • wa can be the header line or it can be any field string having the same structure as a row in the body.
  • if you don't specify a work area, by default  the system uses the header line. In effect, the header line is the default work area.
  • After append, SY-TABIX is set to the relative row number of the row just appended. For example, after appending the first row, SY-TABIX will be set to 1. After appending second row, SY-TABIX will be 2, and so on.
The statement append itab to itab[] appends the header line named itab to the body named itab[]. The statement append itab does the same thing, because the default work area is the header line. Being more succint, the latter is usually used.
A work area mentioned explicitly in the append statement is known as an explicit work area. Any field string having the same structure as a row of the internal table can be used as an explicit work area. If a work area is not mentioned, the implicit work area (the header line) is used.

The below program adds the 5 rows to the internal table.

DATA : BEGIN OF ITAB OCCURS 5,
             A TYPE I,                                            
             B TYPE I,                                                    
             END OF ITAB.

ITAB-A = 1.
ITAB-B = 10.
APPEND ITAB.
WRITE : / 'SY-TABIX = ', SY-TABIX.

ITAB-A = 2.
ITAB-B = 20.
APPEND ITAB.
WRITE : / 'SY-TABIX = ', SY-TABIX.

ITAB-A = 3.
ITAB-B = 30.
APPEND ITAB.
WRITE : / 'SY-TABIX = ', SY-TABIX.

ITAB-A = 4.
ITAB-B = 40.
APPEND ITAB.
WRITE : / 'SY-TABIX = ', SY-TABIX.

ITAB-A = 5.
ITAB-B = 50.
APPEND ITAB.
WRITE : / 'SY-TABIX = ', SY-TABIX.
------------------------------------------------------------------------------------------------------------
O/P:

SYITABIX = 1
SYITABIX = 2
SYITABIX = 3
SYITABIX = 4
SYITABIX = 5

No comments:

Post a Comment