Home »
ABAP Internal Tables: A Practical Guide
An internal table holds rows of the same type in the memory of an ABAP program. Choose the table type by the way you read it, then use the example below that fits your task.
Which table type?
| Type | Access | Choose it when |
|---|---|---|
STANDARD TABLE |
By index; key reads search row by row | You mostly loop, or the table is small |
SORTED TABLE |
By key (binary search) and by index | You read by key or key range, or loop over a part |
HASHED TABLE |
By full unique key, independent of size | You read single rows by a unique key |
A standard table with a secondary key combines a fixed row order with fast key access.
TYPES: BEGIN OF ts_item,
id TYPE i,
grp TYPE c LENGTH 2,
END OF ts_item.
DATA lt_items TYPE STANDARD TABLE OF ts_item
WITH EMPTY KEY
WITH NON-UNIQUE SORTED KEY by_group COMPONENTS grp.
LOOP AT lt_items ASSIGNING FIELD-SYMBOL(<ls_item>)
USING KEY by_group WHERE grp = 'A1'.
ENDLOOP.
What do you want to do?
| Task | Example |
|---|---|
| Count rows, check existence, find a position | LINES, LINE_EXISTS and LINE_INDEX |
| Read a row that may be missing | Fix CX_SY_ITAB_LINE_NOT_FOUND |
| Change rows in a loop | Field symbols |
| Copy only matching rows | FILTER on a standard table |
| Sort and keep the order of equal rows | SORT STABLE |
| Sum values by key | COLLECT |
| Use the rows in a SELECT | JOIN @itab vs. FOR ALL ENTRIES |
| Build a range table | Range table from SELECT |
| Show the table to a user | CL_SALV_TABLE |
| Print it as text | ASCII text table |
| Read it from CSV or JSON | CSV · JSON |
Three rules that prevent most problems
- Give every table a key on purpose.
WITH EMPTY KEYis better than an accidental standard key. - A key read on a large standard table inside a loop is a performance problem. Use a sorted, hashed or secondary key.
- After a read, always handle the case that nothing was found.
Sources: Secondary keys · Table functions