Internal tables are structures which are used to store temporary data during processing of an ABAP program.
Following are the different kind of internal tables
- STANDARD
- SORTED
- HASHED
| Category | Internally managed by | Access | Primary table key | When to use | Hints |
|---|---|---|---|---|---|
STANDARD | Primary table index (that’s why these tables are called index tables) | Table indexTable key | Always non-unique, i.e. duplicate entries are always allowedDefinition of an empty key is possible if the key is not relevant(WITH EMPTY KEY) | If you primarily access the table content for sequential processing or via the table index.Response time for accessing the table using the primary key: This kind of table access is optimized only for sorted and hashed tables. For standard tables, primary key access uses a linear search across all lines. That means that large standard tables (more than 100 lines) are not ideal if the you primarily access the table using the table key. | There is no particular sort order, but the tables can be sorted using SORT.Populating this kind of table: Lines are either appended at the end of the table or inserted at a specific position.Secondary table keys can be defined to make key access to standard tables more efficient.Standard and sorted tables have the least administration costs (F1 docu for standard ABAP). |
SORTED | Primary table index (that’s why these tables are called index tables) | Table indexTable key | Non-uniqueUnique … used to sort the table in ascending order. | Enables an optimized access to table content using table key and index.If access via table key is the main access method, but no unique key can be defined. | Sorting is done automatically when lines are inserted or deleted. As a consequence, the table index must usually be reorganized.The response time for accessing the table using the primary key depends logarithmically on the number of table entries, since a binary search is used.Standard and sorted tables have the least administration costs. |
HASHED | Hash algorithm | Table keySecondary table index | Always unique | For large internal tables.Optimized for key access. Access to table content via table key is the main access method and a unique key can be defined. | The response time for primary key access is constant and independent of the number of entries in the table.Hashed tables have the highest administration costs. |
Table Keys
- Table keys …
- are intended to provide an optimized access to the content of internal tables.
- are either unique or non-unique, i.e. more than one line with the same key (duplicates) can exist in the internal table or not. Regarding the primary table key, the definition depends on the table category. For the secondary table key, the definition depends on the key type. For standard tables, the primary table key can also be defined as empty, i.e. it does not contain any key columns. Note that for standard tables, an optimized access is only possible with secondary table keys.
Populating Internal Tables
Copying internal tables
The example below assumes that the source and target table have compatible line types
itab = itab2.
DATA(itab3) = itab.
itab = CORRESPONDING #( itab3 ).
MOVE-CORRESPONDING itab3 TO itab.
itab = CORRESPONDING #( itab3 EXCEPT e ).
itab = CORRESPONDING #( itab2 DISCARDING DUPLICATES ).
itab_nested2 = CORRESPONDING #( DEEP itab_nested1 ). "nested internal tables
To create an internal table by copying data from another internal table and filtering out lines that do not meet the WHERE condition, you can use the FILTER operator.
DATA(f1) = FILTER #( itab1 WHERE num >= 3 ).
"USING KEY should be sorted hashed or must have a secondary key
DATA(f2) = FILTER #( itab1 USING KEY primary_key WHERE num >= 3 )
Insert and append
APPEND VALUE #( comp1 = a comp2 = b ... ) TO itab.
APPEND lv_struc TO itab.
INSERT VALUE #( comp1 = a comp2 = b ... ) INTO TABLE itab.
INSERT lv_struc INTO TABLE itab.
"Adding all from another Internal table
APPEND LINES OF itab2 TO itab.
INSERT LINES OF itab2 INTO TABLE itab.
"i1/i2 represent integer values
APPEND LINES OF itab2 FROM i1 TO i2 TO itab.
APPEND LINES OF itab2 FROM i1 TO itab.
APPEND LINES OF itab2 TO i2 TO itab.
INSERT LINES OF itab2 FROM i1 TO i2 INTO TABLE itab.
Value Operator
In the example below, the internal table is populated by assigning an internal table that is constructed inline with the VALUE operator. The inline constructed table has two lines. line represents an existing structure with a compatible line type. The other line is constructed inline.
The extra pair of parentheses represents a table line. The
#character indicates that the line type can be derived from the context. The assignment deletes the existing content of the internal table on the left side.
itab = VALUE #( ( line )
( comp1 = a comp2 = b ... ) ).
Reading the internal table
"Primary table index is used by default
READ TABLE itab INTO wa INDEX i.
READ TABLE itab ASSIGNING <fs1> ... "The field symbol must have an appropriate type.
READ TABLE itab ASSIGNING FIELD-SYMBOL(<fs2>) ... "The field symbol is created inline.
Sorting Internal Tables
"Implicit sorting by primary table key and in ascending order by default
SORT itab.
"Optional additions to determine the sort order
"As mentioned above, ASCENDING is used implicitly. Here, specifying it explicitly.
SORT itab ASCENDING.
SORT itab DESCENDING
Modifying Internal Table Content
"line: existing line including key values
MODIFY TABLE it FROM line.
"line constructed inline
MODIFY TABLE it FROM VALUE #( a = 1 b = 2 ... ).
"Respecting only specified fields with the addition TRANSPORTING
"In case of sorted/hashed tables, key values cannot be specified.
MODIFY TABLE it FROM line TRANSPORTING b c.
"Modification via index
"Note that it is only MODIFY, not MODIFY TABLE.
"Example: It modifies the line with number 1 in the primary table index.
MODIFY it FROM line INDEX 1.
Deleting
"Example: The first line in the table is deleted.
DELETE it INDEX 1.
"Implicitly using the primary table key
DELETE ADJACENT DUPLICATES FROM it.
"Deletion respecting the values of the entire line
DELETE ADJACENT DUPLICATES FROM it COMPARING ALL FIELDS.
"Only lines are deleted with matching content in specific fields
DELETE ADJACENT DUPLICATES FROM it COMPARING a c.
Grouping Internal Tables
Coming Soon