-2

I see in many existing ABAP programs that standard internal tables are declared without key fields, for instance:

TYPES type_itab TYPE STANDARD TABLE OF sflight WITH DEFAULT KEY.
DATA itab_0 TYPE type_itab.
DATA itab_1 TYPE TABLE OF sflight.
DATA itab_2 TYPE STANDARD TABLE OF sflight.
DATA itab_3 TYPE STANDARD TABLE OF sflight WITH DEFAULT KEY.
DATA itab_4 TYPE STANDARD TABLE OF sflight WITH EMPTY KEY.
etc.

And one line is usually read this way (with x being the number of any of the internal tables above):

SORT itab_x BY carrid.
READ TABLE itab_x WITH KEY carrid = 'MH' BINARY SEARCH...

So far, so good.

Now, it's theoretically possible to declare some fields explicitly in the key, for standard internal tables (usually, the key fields are declared only for sorted and hashed tables):

DATA itab_5 TYPE STANDARD TABLE OF sflight WITH KEY carrid.

Reading the line with the above code is still possible, and it's the same performance.

So, what is the interest of declaring key fields explicitly in STANDARD internal tables?

1 Answer 1

1

The primary table key of standard tables is not used for the performance, but used by specific processing statements to not repeat the key fields (with wa declared by DATA wa TYPE sflight):

  1. COLLECT wa INTO itab_5. (ABAP documentation: link)

    It locates the line which corresponds to the declared key fields. If the line doesn't exist, the numeric fields which are not key fields are added to the existing line, otherwise wa is inserted. Note that COLLECT works only if all the internal table fields are key fields or if the non-key fields are all numeric.

    "This statement inserts the content of a work area wa either as a single line in an internal table itab or adds the values of its numeric components to the corresponding values of existing lines with the same primary table key."

  2. DELETE TABLE itab_5 FROM wa... (ABAP documentation: link)

    It locates the first line which corresponds to the declared key fields of wa. If the line exists, it's deleted.

    "The first line found in the internal table is processed whose values in the columns of the table key used match those of the corresponding components of wa"

  3. MODIFY TABLE itab_5 FROM wa... (ABAP documentation: link)

    It locates the line which corresponds to the declared key fields of wa (and if the line exists, the non-key fields of wa are copied to this line).

    "In this variant, the statement MODIFY assigns the content of work area wa to a line specified by a table key in table_key or a line number in index"

  4. READ TABLE itab_5 FROM wa... (ABAP documentation: link)

    It locates the line which corresponds to the declared key fields of wa (and if the line exists, it's returned).

    "Specifies a table key as a search key. The primary table key or a secondary table key can be specified. The values can be declared either implicitly in a work area wa behind FROM or explicitly by listing the components of the table key behind TABLE KEY."

  5. SORT itab_5. (ABAP documentation: link)

    "If no explicit sort key is specified using the addition BY, the internal table itab is sorted by the primary table key."

Appendix

Note that, if the internal table has no key fields explicitly defined, the primary table key can be:

  • the standard key. It's formed from all components with character-like and byte-like data types. Examples of standard keys:
    TYPES type_itab TYPE STANDARD TABLE OF sflight WITH DEFAULT KEY.
    DATA itab_0 TYPE type_itab.
    DATA itab_1 TYPE TABLE OF sflight.
    DATA itab_2 TYPE STANDARD TABLE OF sflight.
    DATA itab_3 TYPE STANDARD TABLE OF sflight WITH DEFAULT KEY.
    
  • the empty key. In that case, the above statements are forbidden or have weird behaviors (COLLECT is possible only if all internal table components are numeric and it works on the first line only), DELETE and MODIFY affect the first line, READ TABLE, SORT doesn't sort). Example of empty key:
    DATA itab_4 TYPE STANDARD TABLE OF sflight WITH EMPTY KEY.
    

Also, only the primary table key can be standard, secondary table keys cannot be standard, they can be only sorted and hashed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.