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 - key

Quick Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


... { [UNIQUE | NON-UNIQUE] 
      { {KEY [primary_key [ALIAS key_name] COMPONENTS] comp1 comp2 ...}
      | {DEFAULT KEY} }  }
  | { EMPTY KEY } ...

Alternatives

1. ... [UNIQUE|NON-UNIQUE] {KEY ...}|{DEFAULT KEY}

2. ... EMPTY KEY

Alternative 1

... [UNIQUE|NON-UNIQUE] {KEY ...}|{DEFAULT KEY}

Effect

Defines the primary table key of an internal table by specifying components or defines it as a standard key. The syntax and semantics of the additions are the same as for the statement TYPES for standalone table types, with the difference that the primary key of a bound table type must always be specified completely:

  • In standard tables, only the addition NON-UNIQUE KEY can be specified. If uniqueness is not specified, this is added implicitly. The addition UNIQUE KEY cannot be specified.
  • In sorted tables, one of the two additions UNIQUE KEY or NON-UNIQUE KEY must be specified.
  • In hashed tables, the addition UNIQUE KEY must be specified.

If no primary key is specified for DATA using WITH, the addition WITH NON-UNIQUE DEFAULT KEY is added implicitly for standard tables. This gives the table a standard key, which can be empty. In sorted tables and hashed tables, the primary key must be specified explicitly and cannot be empty.

If the name of the primary key primary_key is specified explicitly, the addition WITH HEADER LINE can no longer be specified, even outside the classes.


Note

The declaration of the primary table key as a standard key can be critical for various reasons. It is best to specify key fields explicitly instead. In particular, make sure that the declaration of the standard key is not added by mistake because the key was not specified explicitly.


Example

Defines a sorted table with a primary key without an explicitly specified name.

DATA sbook_tab 
     TYPE SORTED TABLE 
     OF sbook 
     WITH UNIQUE KEY carrid connid fldate bookid.

Example

Defines a sorted table with a primary key with an explicitly specified name.

DATA sbook_tab 
     TYPE SORTED TABLE 
     OF sbook 
     WITH UNIQUE KEY primary_key 
                 COMPONENTS carrid connid fldate bookid.

Example

Define a sorted table with a primary key, which an alias name is defined for.

DATA sbook_tab 
     TYPE SORTED TABLE 
     OF sbook 
     WITH UNIQUE KEY primary_key ALIAS full_table_key 
                COMPONENTS carrid connid fldate bookid. 

Alternative 2

... EMPTY KEY

Effect

Defines an empty primary key in an internal table. This variant is possible for standard tables only. The syntax and semantics are the same as for the statement TYPES.


Notes

  • The addition EMPTY KEY can clarify situations where the definition of a table key is not important.
  • In general, EMPTY KEY is recommended instead of not specifying a key definition, since otherwise the standard key is used, which can often produce unexpected results.

Example

Declaration of a table with an empty primary key. Using the SORT statement without specifying a sort criterion would have no effect for this table.

DATA unsorted_carrier_list 
     TYPE STANDARD TABLE 
     OF scarr 
     WITH EMPTY KEY.