SAP NetWeaver AS ABAP Release 740, ©Copyright 2014 SAP AG. All rights reserved.
ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Internal Tables → Processing Statements for Internal Tables →Syntax
APPEND line_spec TO itab [SORTED BY comp] [result].
Addition:
Effect
This statement appends one or more rows line_spec to an internal index table itab. It is appended so that a new last row is created with regard to the primary table index.
If itab is a standard table, SORTED BY can be used to sort the table in a specified way. Use result when appending a single row to set a reference to the appended row in the form of a field symbol or a data reference.
For the individual table types, appending is done as follows:
Exceptions are raised in the following cases:
System Fields
The APPEND statement sets sy-tabix to the row number of the last appended row in the primary table index.
Notes
... SORTED BY comp
Effect
Used correctly, this addition can produce ranking lists in descending order. This only works if a value greater than 0 is specified in the declaration of the internal table in the addition INITIAL SIZE. If the value 0 is specified for INITIAL SIZE, the statement APPEND has no effect when used with the addition SORTED BY.
The addition SORTED BY can be used only when a work area wa is specified and for a standard table. Also, wa must be compatible with the row type of the table. The component comp can be specified as shown in the section Specifying Components, however only a single component can be addressed using the object component selector, and no attributes of classes.
As long as the declaration of the internal table for INITIAL SIZE has a value greater than zero, the statement is executed in two steps:
When using only the statement APPEND with addition SORTED BY to fill an internal table, this rule results in an internal table that contains no more than the number of rows specified in its definition after INITIAL SIZE and that is sorted in descending order with regard to the primary table index by component comp (ranking).
Programming Guideline
Create ranking lists with unsorted filling
Example
Creates a ranking of the three flights of a connection showing the most free seats.
PARAMETERS: p_carrid TYPE sflight-carrid,
p_connid TYPE sflight-connid.
DATA: BEGIN OF seats,
fldate TYPE sflight-fldate,
seatsocc TYPE sflight-seatsocc,
seatsmax TYPE sflight-seatsmax,
seatsfree TYPE sflight-seatsocc,
END OF seats.
DATA seats_tab LIKE STANDARD TABLE OF seats
INITIAL SIZE 3.
SELECT fldate seatsocc seatsmax
FROM sflight
INTO seats
WHERE carrid = p_carrid AND
connid = p_connid.
seats-seatsfree = seats-seatsmax - seats-seatsocc.
APPEND seats TO seats_tab SORTED BY seatsfree.
ENDSELECT.
Catchable Exceptions
CX_SY_ITAB_DUPLICATE_KEY
Non-Catchable Exceptions