Appending Table Lines 

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 wa 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. itab1can be any type of table. The line type of itab1 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 n2] TO itab2.

n1 and n2 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 wamust 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.

Example

DATA: BEGIN OF wa,
 col1(1) 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 list 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 list 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(1) TYPE c,
 col2 TYPE i,
 END OF line.

DATA: itab1 LIKE TABLE OF line,
 jtab LIKE itab.

DO 3 TIMES.
 line-col1 = sy-index. line-col2 = sy-index ** 2.
 APPEND line TO itab1.
 line-col1 = sy-index. line-col2 = sy-index ** 3.
 APPEND line TO jtab.
ENDDO.

APPEND LINES OF jtab FROM 2 TO 3 TO itab1.

LOOP AT itab1 INTO line.
 WRITE: / line-col1, line-col2.
ENDLOOP.

The list 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 line3,
 col1 TYPE i,
 col2 TYPE i,
 col3 TYPE i,
  END OF line3.

DATA itab2 LIKE TABLE OF line3 INITIAL SIZE 2.

line3-col1 = 1. line3-col2 = 2. line3-col3 = 3.
APPEND line3 TO itab2 SORTED BY col2.

line3-col1 = 4. line3-col2 = 5. line3-col3 = 6.
APPEND line3 TO itab2 SORTED BY col2.

line3-col1 = 7. line3-col2 = 8. line3-col3 = 9.
APPEND line3 TO itab2 SORTED BY col2.

LOOP AT itab2 INTO line3.
 WRITE: / line3-col2.
ENDLOOP.

The list 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 to 2 through the INITIAL SIZE addition.