ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Internal Tables → Processing Statements for Internal Tables
APPEND
Other versions: 7.31 | 7.40 | 7.54
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. result
can be used when inserting a single row to set a reference to the inserted row in the form of a field symbol or a data reference.
For the individual table types, appending is done as follows:
- To standard tables, rows are appended directly and without checking the content of the internal table.
- Rows are appended to sorted tables only if they match the sort order and do not create duplicate entries (if the primary table key is unique).
-
No rows can be appended to hashed tables.
Exceptions are raised in the following cases:
- If a row to be appended would produce a duplicate entry in a unique primary table key, a non-handleable exception is raised.
- If a row to be appended would produce a duplicate entry in a unique secondary table key, a handleable exception of the class CX_SY_ITAB_DUPLICATE_KEY is raised.
- If a block of rows to be appended would produce a duplicate entry in a unique secondary table key, a non-handleable exception is raised.
-
If a row to be appended would destroy the sort sequence of sorted tables, a non-handleable exception is raised (the secondary index of a sorted secondary key, however, is updated before it is used again).
System Fields
The statement APPEND
sets sy-tabix
to the row number of the last appended row in the primary table index.
Notes
- The administration of a unique secondary table key is updated immediately; the adminstration of a non-unique key is not updated until the secondary table key is next used explicitly (lazy update). Runtime costs for creating or updating a non-unique secondary table key are not incurred therefore until it is used for the first time.
-
The value
operator
VALUE
can also be used to construct the content of internal tables.
Example
Appends 100 random numbers to the internal table itab
with row type i
.
DATA itab TYPE TABLE OF i WITH EMPTY KEY.
DATA(rnd) = cl_abap_random_int=>create( seed = + sy-uzeit
min = 1
max = 100 ).
DO 100 TIMES.
APPEND rnd->get_next( ) TO itab.
ENDDO.
Addition
... 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
is ignored 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:
-
Starting from the final row, the table is scanned for a row in which the value of the component
comp is greater than or equal to the value of the component
comp
ofwa
. If a row like this exists, the work areawa
is inserted after this row with respect to the primary index. In no such row is found, the work areawa
is inserted before the first row with respect to the primary index. The row numbers of all rows after the inserted row are increased by 1 in the primary table index. -
If the number of rows before the statement is executed is greater than or equal to the number specified
in the declaration of the internal table in the addition
INITIAL SIZE
, the new final row is deleted with respect to the primary table index.
When using only the statement APPEND
with the addition SORTED
BY to fill an internal table with a value no greater than 0 for INITIAL SIZE
,
this rule produces 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 respect 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.
DATA: carrid TYPE sflight-carrid VALUE 'LH',
connid TYPE sflight-connid VALUE '0400'.
cl_demo_input=>new(
)->add_field( CHANGING field = carrid
)->add_field( CHANGING field = connid )->request( ).
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, seatsmax - seatsocc AS seatsfree
FROM sflight
WHERE carrid = @carrid AND
connid = @connid
INTO @seats.
APPEND seats TO seats_tab SORTED BY seatsfree.
ENDSELECT.
cl_demo_output=>display( seats_tab ).
Exceptions
Handleable Exceptions
CX_SY_ITAB_DUPLICATE_KEY
-
Cause: Duplicate key values in unique secondary key
Runtime error:ITAB_DUPLICATE_KEY
Non-Handleable Exceptions
-
Cause: Row with identical key inserted (target table defined using
UNIQUE
)
Runtime error:ITAB_DUPLICATE_KEY_IDX_OP
-
Cause: Sort order violated after an
APPEND
on a sorted table
Runtime error:ITAB_ILLEGAL_SORT_ORDER
: -
Cause: Invalid index value (<= 0) when
FROM
,TO
, orINDEX
specified
Runtime error:TABLE_INVALID_INDEX
-
Cause: Memory area violated when
TABLES
parameter accessed
Runtime error:ITAB_STRUC_ACCESS_VIOLATION