Section 5 of 11

Internal Tables

The three internal table kinds and their keys, statements for reading and changing rows, and the performance implications.

Open contents

Internal tables are the central data structure in ABAP. Most of what an ABAP program does is manipulate them.

Three kinds

Internal table kinds
KindKeySearchDuplicatesUse
STANDARDNon-uniqueLinearAllowedSequential processing, index access
SORTEDUnique or non-uniqueBinaryConfigurableFrequent key lookups, always sorted
HASHEDUnique requiredHashNot allowedFast lookup by full key

Adding rows

Insertion
APPEND ls_item TO lt_items.
INSERT ls_item INTO TABLE lt_sorted.
APPEND LINES OF lt_source TO lt_target.

DATA(lt_items) = VALUE ty_items(
  ( matnr = 'MAT-001' menge = 10 )
  ( matnr = 'MAT-002' menge = 20 ) ).

APPEND is not available on sorted or hashed tables, because position is determined by the key rather than by insertion order.

Reading rows

Reading
READ TABLE lt_items INTO ls_item WITH KEY matnr = 'MAT-001'.
IF sy-subrc = 0.
ENDIF.

READ TABLE lt_items ASSIGNING FIELD-SYMBOL(<fs>) WITH KEY matnr = 'MAT-001'.

READ TABLE lt_items TRANSPORTING NO FIELDS WITH KEY matnr = 'MAT-001'.

DATA(ls_found) = lt_items[ matnr = 'MAT-001' ].

Changing and deleting

Modification
MODIFY lt_items FROM ls_item TRANSPORTING menge WHERE matnr = 'MAT-001'.

DELETE lt_items WHERE menge = 0.

SORT lt_items BY matnr.
DELETE ADJACENT DUPLICATES FROM lt_items COMPARING matnr.

CLEAR lt_items.
FREE  lt_items.

Aggregating

Control level processing
SORT lt_items BY matnr.
LOOP AT lt_items INTO ls_item.
  AT NEW matnr.
    lv_subtotal = 0.
  ENDAT.

  lv_subtotal = lv_subtotal + ls_item-menge.

  AT END OF matnr.
    WRITE: / ls_item-matnr, lv_subtotal.
  ENDAT.
ENDLOOP.

DATA(lv_total) = REDUCE i( INIT s = 0
                           FOR ls IN lt_items
                           NEXT s = s + ls-menge ).

๐Ÿ“– Unfamiliar term? Look it up in the SAP glossary.

Check your understanding

Test what you just read.

Quiz 1

Which type of internal table provides the fastest direct access by key?

Quiz 2

A SORTED TABLE automatically maintains its data sorted by key fields.

Quiz 3

Which statement adds a row to the end of an internal table?

Quiz 4

Which statement reads a single row from an internal table that matches a condition?

Quiz 5

HASHED TABLE supports index-based access.

Quiz 6

Arrange the internal table data operations in their typical processing order.

Click items in the correct order