Skip to content

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

TYPES - tabkeys

Quick Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


... [ WITH key ] 
    [ WITH secondary_key1 ] [ WITH secondary_key2 ] ...
    [ {WITH|WITHOUT} FURTHER SECONDARY KEYS ] ...

Extras

1. ... WITH FURTHER SECONDARY KEYS

2. ... WITHOUT FURTHER SECONDARY KEYS

Effect

Defines the table key of a table type. The following can be defined:

The order in which the components of a table key are defined is significant for the table type. The additions WITH|WITHOUT FURTHER SECONDARY KEYS determine the genericness with respect to the secondary table key.

An internal table that has no table key or an incomplete table key is generic with respect to the table key. A table type of this nature can be used only for typing formal parameters or field symbols. For DATA, a standard table type with a generic primary table key can be specified after TYPE. In this case, a bound table type with a standard key is created.

Primary Key

The genericness of a table type with respect to the primary key is determined as follows:

  • If WITH key is not specified, a table type is generic with respect to the primary key.
  • If a declaration about the uniqueness of the key is not made in key, a table type is partly generic with respect to the key.
  • If a key declaration with a uniqueness declaration is made in key, a table type is not generic with respect to the primary key.

The non-generic table categories can be split up as follows:

  • If no primary key is declared for standard tables, this primary key is generic with respect to the key fields and defined implicitly as non-unique.
  • If no primary key is declared for sorted tables, this primary key is generic with respect to the key fields and uniqueness.
  • If no primary key is declared for hashed tables, this primary key is generic with respect to the key fields and uniqueness; note, however, that fixed hash tables can have only one unique key.

Secondary Key

The genericness of a table type with respect to the secondary key depends (by default) on the genericness of the primary key and can be overridden using the additions WITH|WITHOUT FURTHER SECONDARY KEYS.

  • If the primary key is completely or partially generic, then the table type is by default generic with respect to the secondary key. The default behavior can be expressed using the addition WITH FURTHER SECONDARY KEYS. However, if the addition WITHOUT FURTHER SECONDARY KEYS is specified, the table type is generic with respect to the primary key but not with respect to the secondary key.
  • If the primary key is not generic, then the table type is not generic with respect to the secondary key either (by default). The default behavior can be expressed using the addition WITHOUT FURTHER SECONDARY KEYS. If, however, WITH FURTHER SECONDARY KEYS is declared, the table type is generic with respect to the secondary key but not with respect to the primary key.


Note

A generic table type cannot be used as a component of a structured type.


Example

Defines a table type for a hashed table with a unique primary key plus two non-unique secondary sorted keys, cityfrom_to and airp_from_to.

TYPES spfli_tab TYPE HASHED TABLE OF spfli 
  WITH UNIQUE KEY            carrid connid 
  WITH NON-UNIQUE SORTED KEY cityfrom_to  COMPONENTS cityfrom cityto 
  WITH NON-UNIQUE SORTED KEY airp_from_to COMPONENTS airpfrom airpto. 

Addition 1

... WITH FURTHER SECONDARY KEYS

Effect

This addition defines the table type explicitly as being generic with respect to the secondary key. This means it includes table types that can have further secondary keys and not just the optional secondary keys declared using WITH secondary_key ....

If 15 secondary keys are already defined, the addition WITH FURTHER SECONDARY KEYS cannot be specified.


Notes

  • This addition expresses the default behavior in table types with a generic primary key.
  • In table types with a non-generic primary key, WITH FURTHER SECONDARY KEYS can be used to override the default behavior make it generic with respect to the secondary keys.

Addition 2

... WITHOUT FURTHER SECONDARY KEYS

Effect

This addition defines the table type explicitly as being generic with respect to the secondary key. This means it includes only table types that have precisely those secondary keys specified using WITH secondary_key ....


Note

This addition expresses the default behavior in table types with a generic primary key.


Example

The addition WITH FURTHER SECONDARY KEYS in the declaration of the tabular type itab makes it possible to pass internal tables with any secondary keys to the parameter p of the method meth. If this addition is not used, the addition WITHOUT FURTHER SECONDARY KEYS is used implicitly and the method call shown is not possible.

CLASS demo DEFINITION. 
  PUBLIC SECTION. 
    TYPES itab TYPE SORTED TABLE OF scarr 
               WITH UNIQUE KEY carrid 
               WITH FURTHER SECONDARY KEYS. 
    CLASS-METHODS meth IMPORTING p TYPE itab. 
ENDCLASS. 

CLASS demo IMPLEMENTATION. 
  METHOD meth. 
  ENDMETHOD. 
ENDCLASS. 

DATA itab TYPE SORTED TABLE OF scarr 
          WITH UNIQUE KEY carrid 
         WITH NON-UNIQUE SORTED KEY name COMPONENTS carrname. 

START-OF-SELECTION. 
  demo=>meth( itab ). 

Continue

TYPES - key

TYPES - secondary_key