ABAP Keyword Documentation → ABAP - Reference → Processing Internal Data → Internal Tables → Internal Tables - Overview → Table Keys
Secondary Table Key
Hash keys and sorted keys can be declared as secondary table keys for each internal table. For each sorted key, an additional secondary table index is created.
Access to an internal table using a secondary key is always optimized. This allows additional optimized keys to be introduced for sorted and hashed tables as well as optimized key accesses for standard tables.
- Declaration of Secondary Table Keys
UNIQUE|NON-UNIQUE KEY key_name COMPONENTS
of statements
TYPES
, DATA
and so on. For table types created in the ABAP Dictionary, the tool provides the corresponding functions.
- Access Using Secondary Keys
WITH [TABLE] KEY key_name
can be used to specify which secondary table key to use. During index accesses, you can use USING KEY keyname
to specify which secondary key's
table index to use. Secondary keys are not selected automatically. If no secondary key is specified during a
processing statement, the primary key or primary table index is always used. Statements where secondary keys can be specified are:
READ TABLE itab
The rows to be read can be specified using a secondary key.
LOOP AT itab
The processing sequence and conditions can be controlled using a secondary table key.
INSERT itab
Only a secondary key for the source table can be specified here, from which multiple rows are copied. The position they are inserted at is determined solely using the primary key and the primary index.
APPEND
Only a secondary key for the source table can be specified here, onto which multiple rows are appended.
MODIFY itab
The rows to be modified can be specified using a secondary key.
DELETE itab
The rows to be deleted can be specified using a secondary key.
sy-tabix
is set by this type of access, and a sorted secondary key is used, the row number refers to the associated
secondary table index.
In statements that these additions has not been introduced for, like
SORT, COLLECT
, or
PROVIDE
, secondary keys are not explicitly supported.
The key fields of a secondary table key are only write-protected if the secondary table key is in use
in a LOOP
loop or in a MODIFY
statement. Otherwise, the secondary key fields are not write-protected.
Other versions: 7.31 | 7.40 | 7.54
Programming Guideline
Avoid using deep inheritance hierarchies
Notes
- Optimized access times for the individual rows via secondary keys are bought in exchange for the fact that the ABAP runtime environment then needs to administer the additional keys. For hash keys, this means additional hash administration. For each sorted key, it means an additional secondary table index.
- When working with internal tables that a secondary key is declared for, you always have to make sure that the required key or table index is used in the processing statements.
Example
Program DEMO_SECONDARY_KEYS demonstrates the specification of a secondary table key and the resulting performance gain.