Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Internal Tables →  Expressions and Functions for Internal Tables →  table_exp - Table Expressions 

table_exp - itab_line

Other versions: 7.31 | 7.40 | 7.54

Syntax


... { [KEY keyname INDEX] idx } 
  | { comp1 = operand1 comp2 = operand2 ...  }
  | { KEY keyname [COMPONENTS] {comp_name1|(name1)} = operand1
                               {comp_name2|(name2)} = operand2
                                ... } ...

Alternatives

1. ... [KEY keyname INDEX] idx

2. ... comp1 = operand1 comp2 = operand2 ...

3. ... KEY keyname [COMPONENTS] ...

Effect

Specifies a table row in the square brackets of a table expression. This makes three alternatives possible:

  • Index read
  • Read using a free key
  • Read using a table key

Executable Example

Table Expressions, Specified Rows

Alternative 1

... [KEY keyname INDEX] idx

Effect

The table expression reads the row with the row number specified in idx with respect to a table index. The row is read in exactly the same way as when specifying an index, INDEX idx [USING KEY keyname], in the statement READ TABLE. Similarly, idx is a numeric expression position and the optional KEY keyname INDEX is equivalent to the USING KEY keyname specified here. If the optional addition is not used, itab must be an index table. If the addition is used, the secondary table index of a sorted secondary key can be specified statically or dynamically.

If no row exists for the specified index, the handleable exception CX_SY_ITAB_LINE_NOT_FOUND is usually raised. The attributes KEY_NAME and KEY_COMP_VALUES are not filled here. Exceptions to this rule are when a start value is specified, uses in the statement ASSIGN, in the predicate function line_exists, and in the table function line_index.


Example

Reads the row with the row number 1 once using the secondary table key skey and twice using the primary table key. It is the third row once and the first row twice.

DATA itab TYPE TABLE OF i 
          WITH NON-UNIQUE KEY primary_key COMPONENTS table_line 
          WITH NON-UNIQUE SORTED KEY skey COMPONENTS table_line. 

itab = VALUE #( ( 3 ) ( 2 ) ( 1 ) ). 

cl_demo_output=>new( 
  )->write( itab[ KEY skey INDEX 1 ] 
  )->write( itab[ KEY primary_key INDEX 1 ] 
  )->write( itab[ 1 ] 
  )->display( ). 

Alternative 2

... comp1 = operand1 comp2 = operand2 ...

Effect

The table expression reads the row in accordance with the specified free search key. The search is performed in exactly the same way as when specifying the free search key comp1 = operand1 comp2 = operand2 ... in the statement READ TABLE. Similarly, the components comp1 comp2 ... can be specified in accordance with the rules from the section Specifying Components. Also, compatible or convertible operands operand1 operand2 ... must be specified that are general expression positions.

If no row is found for the specified free key, the handleable exception CX_SY_ITAB_LINE_NOT_FOUND is usually raised. The attributes KEY_NAME and KEY_COMP_VALUES are not filled here. Exceptions to this rule are when a start value is specified, uses in the statement ASSIGN, in the predicate function line_exists, and in the table function line_index.


Notes

  • If the free key overlaps with some or all of the primary table key, the optimizations described under READ TABLE are performed when sorted tables and hashed tables are read.

  • Unlike the statement READ TABLE with a free key specified, no binary searches can be forced for a table expression and it is not possible to specify explicit table key for optimizing searches using secondary keys.

Example

Reads a row in an internal table using a free specified key.

DATA itab TYPE SORTED TABLE OF spfli 
          WITH UNIQUE KEY carrid connid. 

SELECT * 
       FROM spfli 
       INTO TABLE @itab. 

TRY. 
    cl_demo_output=>display( itab[ cityfrom = '...' 
                                 cityto   = '...' ]  ). 
  CATCH cx_sy_itab_line_not_found. 
    ... 
ENDTRY. 

Alternative 3

... KEY keyname [COMPONENTS] ...

Effect

The table expression reads the row in accordance with the explicitly specified table key.

  • Generally, the search is performed in exactly the same way as when specifying the table key WITH TABLE KEY [keyname COMPONENTS] ... in the statement READ TABLE. The table key must be covered completely by specifying components and no components can be specified that are not part of the table key.
  • If the table expression is specified as an argument of the predicate function line_exists or of the table function line_index, the search is performed as when specifying a free search key WITH KEY keyname COMPONENTS ... in the statement READ TABLE and the key is joined with a table key. A sorted table key does not have to be covered in full by specified components. For both categories of secondary keys, components can be specified that are not part of the table key.

Unlike the statement READ TABLE, a table expression must specify the name of the table key and the addition COMPONENTS can be omitted. Like here, the table key keyname and the key components can be specified statically or dynamically. The operands operand1 operand2 ... are general expression positions.

If no row is found for the specified table key, the handleable exception CX_SY_ITAB_LINE_NOT_FOUND is usually raised. The attributes KEY_NAME and (if the table is not empty) KEY_COMP_VALUES are not filled here. Exceptions to this rule are when a start value is specified, uses in the statement ASSIGN, in the predicate function line_exists, and in the table function line_index.


Notes

  • Before the primary table key can be used explicitly in this variant, the key must first be specified using its predefined name primary_key or an alias name.

  • Searches using the primary table key do not have to specify it explicitly. Instead, the variant with a free search key can be used. If this covers the primary table key, it is used for optimized reads on sorted tables and hashed tables.

  • The differing behavior of the specified key outside and inside the functions line_exists and line_index is explained as follows:

  • Outside the functions, the main purpose of an explicitly specified table key is to read precisely one fully specified row.

  • Within the functions, the existence of a specified row (not necessarily specified in full) or, for example, a row number as the starting point of a subsequent sequential read using LOOP ... FROM is to be determined as efficiently as possible.

Example

Reads a line in an internal table by specifying the primary table key.

DATA itab TYPE SORTED TABLE OF spfli 
          WITH UNIQUE KEY carrid connid. 

SELECT * 
       FROM spfli 
       INTO TABLE @itab. 

TRY. 
    cl_demo_output=>display( itab[ KEY primary_key 
                                     carrid = '...' 
                                      connid = '...' ]  ). 
  CATCH cx_sy_itab_line_not_found. 
    ... 
ENDTRY. 

Continue

Table Expressions, Specified Rows