Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Declarations →  Declaration Statements →  Data Types and Data Objects →  Declaring Data Objects →  DATA →  DATA - TABLE OF →  DATA - tabkeys 

DATA - secondary_key

Quick Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


... {UNIQUE HASHED}|{UNIQUE SORTED}|{NON-UNIQUE SORTED} 
    KEY key_name COMPONENTS comp1 comp2 ...

Effect

Defines a secondary table key of an internal table. The syntax and semantics of the additions are the same as those for the statement TYPES for standalone table types.

Programming Guideline

Use secondary keys in a way that benefits the table.


Notes

  • When internal tables are accessed using the statements READ TABLE itab, LOOP AT itab, MODIFY itab, and DELETE itab or using table expressions and in mesh types and mesh paths, a secondary key can be used to specify the rows to be processed or the processing order. To do this, the additions WITH [TABLE] KEY ... COMPONENTS or USING KEY must be specified in the statements and the addition KEY in table expressions. A secondary key is never used implicitly.
  • The statement INSERT itab determines the insert position using the primary key and primary index only. A secondary key can be specified only for the source table from which multiple rows are copied. The latter also applies to the statement APPEND.
  • If different table keys for an internal table contain the same components, the syntax check issues a warning (which can be hidden using a pragma). In the case of references to a non-generic table type defined using TYPES, any pragma specified here is also applied to the DATA statement. In the case of references to a generic table type for which no primary table key is defined, the specified pragma is not applied to the DATA statement, since this statement uses a complete table type implicitly and the pragma must also be specified in the case of DATA.
  • If a secondary key is defined, the addition WITH HEADER LINE can no longer be specified, even outside classes.
  • For more information, see TYPES.

Example

Declares an internal table with primary key and two secondary keys.

DATA sbook_tab 
     TYPE STANDARD TABLE 
     OF sbook 
     WITH NON-UNIQUE KEY carrid connid fldate bookid 
     WITH UNIQUE HASHED KEY hash_key 
          COMPONENTS carrid connid fldate bookid 
     WITH NON-UNIQUE SORTED KEY sort_key 
          COMPONENTS customid.

Example

As in the previous example but with explicit naming of the primary key.

DATA sbook_tab 
     TYPE STANDARD TABLE 
     OF sbook 
     WITH NON-UNIQUE KEY primary_key 
          COMPONENTS carrid connid fldate bookid 
     WITH UNIQUE HASHED KEY hash_key 
          COMPONENTS carrid connid fldate bookid 
     WITH NON-UNIQUE SORTED KEY sort_key 
          COMPONENTS customid.

Executable Example

The program DEMO_SECONDARY_KEYS demonstrates the declaration and use of a secondary table key and the resulting performance gains.