Start of Content Area

Appending Table Lines  Locate the document in its SAP Library structure

There are several ways of adding lines to index tables. The following statements have no equivalent that applies to all internal tables.

Appending a Single Line

To add a line to an index table, use the statement:

APPEND <line> TO <itab>.

<line> is either a work area that is convertible to the line type, or the expression INITIAL LINE. If you use <wa>, the system adds a new line to the internal table <itab> and fills it with the contents of the work area. INITIAL LINE appends a blank line containing the correct initial value for each field of the structure. After each APPEND statement, the system field SY-TABIX contains the index of the appended line.

Appending lines to standard tables and sorted tables with a non-unique key works regardless of whether lines with the same key already exist in the table. Duplicate entries may occur. A runtime error occurs if you attempt to add a duplicate entry to a sorted table with a unique key. Equally, a runtime error occurs if you violate the sort order of a sorted table by appending to it.

Appending Several Lines

You can also append internal tables to index tables using the following statement:

APPEND LINES OF <itab1> TO <itab2>.

This statement appends the whole of ITAB1 to ITAB2. ITAB1 can be any type of table, but its line type must be convertible into the line type of ITAB2.

When you append an index table to another index table, you can specify the lines to be appended as follows:

APPEND LINES OF <itab1> [FROM <n1>] [TO <n 2>] TO <itab2>.

<n 1 > and <n 2 > specify the indexes of the first and last lines of ITAB1 that you want to append to ITAB2.

This method of appending lines of one table to another is about 3 to 4 times faster than appending them line by line in a loop. After the APPEND statement, the system field SY-TABIX contains the index of the last line appended. When you append several lines to a sorted table, you must respect the unique key (if defined), and not violate the sort order. Otherwise, a runtime error will occur.

Ranked lists

You can use the APPEND statement to create ranked lists in standard tables. To do this, create an empty table, and then use the statement:

APPEND <wa> TO <itab> SORTED BY <f>.

The new line is not added to the end of the internal table <itab>. Instead, the table is sorted by field <f> in descending order. The work area <wa> must be compatible with the line type of the internal table. You cannot use the SORTED BY addition with sorted tables.

When you use this technique, the internal table may only contain as many entries as you specified in the INITIAL SIZE parameter of the table declaration. This is an exception to the general rule, where internal tables can be extended dynamically. If you add more lines than specified, the last line is discarded. This is useful for creating ranked lists of limited length (for example "Top Ten"). You can use the APPEND statement to generate ranked lists containing up to 100 entries. When dealing with larger lists, it is advisable to sort tables normally for performance reasons.

Examples

Example

DATA: BEGIN OF WA,
        COL1 TYPE C,
        COL2 TYPE I,
      END OF WA.

DATA ITAB LIKE TABLE OF WA.

DO 3 TIMES.
  APPEND INITIAL LINE TO ITAB.
  WA-COL1 = SY-INDEX. WA-COL2 = SY-INDEX ** 2.
  APPEND WA TO ITAB.
ENDDO.

LOOP AT ITAB INTO WA.
  WRITE: / WA-COL1, WA-COL2.
ENDLOOP.

The output is:

          0
1         1
          0
2         4
          0
3         9

This example creates an internal table ITAB with two columns that is filled in the DO loop. Each time the processing passes through the loop, an initialized line is appended and then the table work area is filled with the loop index and the square root of the loop index and appended.

Example

DATA: BEGIN OF LINE1,
        COL1(3) TYPE C,
        COL2(2) TYPE N,
        COL3    TYPE I,
      END OF LINE1,
      TAB1 LIKE TABLE OF LINE1.

DATA: BEGIN OF LINE2,
        FIELD1(1)  TYPE C,
        FIELD2     LIKE TAB1,
      END OF LINE2,
      TAB2 LIKE TABLE OF LINE2.

LINE1-COL1 = 'abc'. LINE1-COL2 = '12'. LINE1-COL3 = 3.
APPEND LINE1 TO TAB1.

LINE1-COL1 = 'def'. LINE1-COL2 = '34'. LINE1-COL3 = 5.
APPEND LINE1 TO TAB1.

LINE2-FIELD1 = 'A'. LINE2-FIELD2 = TAB1.
APPEND LINE2 TO TAB2.

REFRESH TAB1.

LINE1-COL1 = 'ghi'. LINE1-COL2 = '56'. LINE1-COL3 = 7.
APPEND LINE1 TO TAB1.

LINE1-COL1 = 'jkl'. LINE1-COL2 = '78'. LINE1-COL3 = 9.
APPEND LINE1 TO TAB1.

LINE2-FIELD1 = 'B'. LINE2-FIELD2 = TAB1.
APPEND LINE2 TO TAB2.

LOOP AT TAB2 INTO LINE2.
  WRITE: / LINE2-FIELD1.
  LOOP AT LINE2-FIELD2 INTO LINE1.
    WRITE: / LINE1-COL1, LINE1-COL2, LINE1-COL3.
  ENDLOOP.
ENDLOOP.

The output is:

A
abc 12          3
def 34          5
B
ghi 56          7
jkl 78          9

The example creates two internal tables TAB1 and TAB2. TAB2 has a deep structure because the second component of LINE2 has the data type of internal table TAB1. LINE1 is filled and appended to TAB1. Then, LINE2 is filled and appended to TAB2. After clearing TAB1 with the REFRESH statement, the same procedure is repeated.

Example

DATA: BEGIN OF LINE,
        COL1 TYPE C,
        COL2 TYPE I,
      END OF LINE.

DATA: ITAB LIKE TABLE OF LINE,
      JTAB LIKE ITAB.

DO 3 TIMES.
  LINE-COL1 = SY-INDEX. LINE-COL2 = SY-INDEX ** 2.
  APPEND LINE TO ITAB.
  LINE-COL1 = SY-INDEX. LINE-COL2 = SY-INDEX ** 3.
  APPEND LINE TO JTAB.
ENDDO.

APPEND LINES OF JTAB FROM 2 TO 3 TO ITAB.

LOOP AT ITAB INTO LINE.
  WRITE: / LINE-COL1, LINE-COL2.
ENDLOOP.

The output is:

1         1
2         4
3         9
2         8
3        27

This example creates two internal tables of the same type, ITAB and JTAB. In the DO loop, ITAB is filled with a list of square numbers, and JTAB with a list of cube numbers. Then, the last two lines of JTAB are appended to ITAB.

Example

DATA: BEGIN OF LINE,
        COL1 TYPE I,
        COL2 TYPE I,
        COL3 TYPE I,
       END OF LINE.

DATA ITAB LIKE TABLE OF LINE INITIAL SIZE 2.

LINE-COL1 = 1. LINE-COL2 = 2. LINE-COL3 = 3.
APPEND LINE TO ITAB SORTED BY COL2.

LINE-COL1 = 4. LINE-COL2 = 5. LINE-COL3 = 6.
APPEND LINE TO ITAB SORTED BY COL2.

LINE-COL1 = 7. LINE-COL2 = 8. LINE-COL3 = 9.
APPEND LINE TO ITAB SORTED BY COL2.

LOOP AT ITAB INTO LINE.
  WRITE: / LINE-COL2.
ENDLOOP.

The output is:

         8
         5

The program inserts three lines into the internal table ITAB using the APPEND statement and the SORTED BY addition. The line with the smallest value for the field COL2 is deleted from the table, since the number of lines that can be appended is fixed through the INITIAL SIZE 2 addition in the DATA statement.