APPEND

APPEND Syntax Diagram

Variants:

1. APPEND [wa TO|INITIAL LINE TO] itab.
2. APPEND LINES OF itab1 [FROM idx1] [TO idx2] TO itab2.
3. APPEND [wa TO] itab SORTED BY f.

Effect

Appends a new line to the end of the internal table itab. You can only use this variant with index tables (standard or sorted table).

If you specify wa TO, the new line is taken from the contents of the explicitly specified work area wa.

If you use INITIAL LINE TO, a line filled with the correct initial value for the type is added.

If the part before itab is omitted, the new line is taken from the header line of the internal table itab.

After the APPEND, the system field SY-TABIX contains the index of the newly added table entry.


If the target table itab is a SORTED TABLE, it must be sorted correctly after the append, otherwise a runtime error results.

TABLES SCUSTOM. 
DATA: CUSTOMER LIKE SCUSTOM-ID OCCURS 0. 
 
APPEND SCUSTOM-ID TO CUSTOMER.

Append a blank line or a line with its initial value to the above list:

APPEND INITIAL LINE TO CUSTOMER

Generate a compressed list with plane data

PARAMETERS: SEATS_LO LIKE SAPLANE-SEATSMAX DEFAULT 30, 
			SEATS_HI LIKE SAPLANE-SEATSMAX DEFAULT 50. 
 
DATA: PLANE		LIKE SAPLANE OCCURS 0, 
	PLANE_NEEDED LIKE SAPLANE WITH HEADER LINE. 
 
LOOP AT PLANE INTO PLANE_NEEDED 
			WHERE SEATSMAX BETWEEN SEATS_LO AND SEATS_HI. 
  APPEND PLANE_NEEDED. 
ENDLOOP.


Variant 2

APPEND LINES OF itab1 [FROM idx1] [TO idx2] TO itab2.

Effect

Appends the internal table itab1 or a block of lines from itab1 to the end of the internal table itab2.
You can only use this variant with index tables (standard or sorted table).

By specifying FROM idx1 or TO idx2 , you can restrict the line area taken from the source table itab1. If there is no FROM specification, it begins with the first line of itab1. If there is no TO specification, it ends with the last line of itab1 . This means that the complete table is appended if neither a FROM nor a TO is specified.

After the APPEND, the system field SY-TABIX contains the index of the last table entry appended, i.e. the total number of entries from both tables.

If the target table itab is a SORTED TABLE, it must be correctly sorted after the append, otherwise a runtime error occurs.

Note

By comparing the values of SY-TABIX before and after the APPEND statement, you can determine how many lines were appended to the table.

DATA: ITAB1  TYPE I OCCURS 100, 
	ITAB2  TYPE I OCCURS 100. 
 
APPEND 2 TO ITAB1. 
APPEND 3 TO ITAB1. 
APPEND 5 TO ITAB1. 
APPEND 7 TO ITAB1. 
 
APPEND 3 TO ITAB2. 
APPEND INITIAL LINE TO ITAB2. 
 
APPEND LINES OF ITAB1 FROM 2 TO 20 TO ITAB2.

The table ITAB2 now contains five lines with the values 3, 0, 3, 5 and 7.


This variant is 3 to 4 times faster than using a LOOP to process the source table and append the entries line-by-line to the target table.

Effect

Inserts the new entry into table and re-sorts the table by the sub-field f in descending order. This only makes sense if the table was sorted beforehand. When the number of table entries reaches the OCCURS parameter value, the last entry is deleted if the value f of a new entry is greater (particularly suitable for ranked lists). You can only sort by one sub-field, and may only use this variant with a STANDARD TABLE.

If you specify wa TO , the new line is taken from the contents of the explicitly specified work area wa. Otherwise, it comes from the header line of the internal table itab.

Example

DATA: BEGIN OF COMPANIES OCCURS 3, 
		NAME(10), SALES TYPE I, 
	END   OF COMPANIES. 
 
COMPANIES-NAME = 'big'. 
COMPANIES-SALES = 90. 
APPEND COMPANIES. 
 
COMPANIES-NAME = 'small'. 
COMPANIES-SALES = 10. 
APPEND COMPANIES. 
 
COMPANIES-NAME = 'too small'. 
COMPANIES-SALES =  5. 
APPEND COMPANIES. 
 
COMPANIES-NAME = 'middle'. 
COMPANIES-SALES = 50. 
APPEND COMPANIES SORTED BY SALES.

The table now has three (-> OCCURS 3) entries. The line with the contents 'too small' in the sub-field NAME is deleted from the table because the entry for 'middle' has a greater value in the sub-field SALES . This entry now appears in the second table line (after 'big' and before 'small').

Whenever an internal table is processed with APPEND SORTED BY, it should always be filled in this way.

Related

COLLECT itab, INSERT itab, SELECT / FETCH NEXT CURSOR ... INTO/APPENDING TABLE itab, MODIFY itab, WRITE f TO itab INDEX idx, SORT itab, READ TABLE itab, LOOP AT itab, DELETE itab