Internal Tables

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

A program often has to hold a list of things for a short time: the items of an expense claim, the rows read from a database table, or the results of a calculation. In ABAP that list is an internal table. It lives only in the memory of the running program, holds any number of rows that all have the same shape, and disappears when the program ends. A newcomer meets internal tables on the first day, because almost every ABAP program reads data into one, changes it and hands it on. This page explains the three kinds of internal table, what a table key is, the statements that add, change and remove rows, why old programs sometimes show tables with a hidden work area, and how the kind of table you choose affects the speed of a lookup. It is written against Standard ABAP release 8.16 and ABAP for Cloud Development release 9.20 (2608). Internal tables, their three kinds and the statements below are part of the core language and apply in ABAP Cloud, in classic Standard ABAP, on premise and in the Public Edition alike; the section on header lines describes an older habit that belongs to classic Standard ABAP and that the code of classes does not use.

Before you read

Key terms

  • Internal table: a table that lives only in the memory of the running program and holds any number of rows of the same type.

  • Table kind: the way a table stores and finds its rows, one of standard, sorted or hashed.

  • Primary key: the columns that identify a row of a table and that ABAP uses for key-based access.

  • Table index: the position number of a row, counted from 1, which standard and sorted tables keep and hashed tables do not.

  • Work area: a single structure that holds one row while you read it from or write it to a table.

  • Header line: a work area with the same name as the table, created implicitly in old programs and not allowed in modern code.

What an internal table is

Picture a spreadsheet that exists only while the program runs. Every row has the same columns, and you decide the columns by giving the table a row type. The row type is usually a structure, for example the shape of one claim item, but it can also be a single elementary type such as i or string, which gives a plain list of values.

The table type, written TYPE STANDARD TABLE OF, TYPE SORTED TABLE OF or TYPE HASHED TABLE OF, fixes three things at once: the row type, the table kind and the primary key. Everything else follows from these three choices. There is no size to declare; the table grows as you add rows.

An example in Expense claims terms is the list of items of one claim. The structure ty_item is one row, and the table type ty_items is the whole list. The statements below declare the types and a table, and one WRITE line shows that the new table is empty. This listing runs in the local playground and prints rows: 0.

TYPES: BEGIN OF ty_item,
         claim_id TYPE c LENGTH 10,
         item_no  TYPE i,
         category TYPE c LENGTH 12,
         amount   TYPE p LENGTH 8 DECIMALS 2,
       END OF ty_item.

TYPES ty_items TYPE STANDARD TABLE OF ty_item WITH EMPTY KEY.

DATA items TYPE ty_items.

WRITE / |rows: { lines( items ) }|.

The three table kinds

ABAP offers three kinds of internal table. They differ in how they keep their rows and therefore in what you can do with them and how fast a lookup is.

  • A standard table keeps rows in the order they were added. Every row has an index (its position, counting from 1). The key does not have to be unique, and a lookup by key reads the rows one after the other.

  • A sorted table keeps its rows ordered by the key at all times. It also has an index. The key may be unique or non-unique, and a lookup by the leading part of the key can skip straight to the right region.

  • A hashed table has no order and no index. Its key must be unique, and a lookup by the complete key goes directly to the row through a hash function, no matter how many rows the table holds.

flowchart TD A[Which table kind?] --> B{Need to find rows by key often?} B -- No --> C[Standard table: append and loop in order] B -- Yes --> D{Unique key and exact lookups only?} D -- Yes --> E[Hashed table: lookup by full key] D -- No --> F[Sorted table: ordered by key, partial key lookups]

Primary keys

The primary key is the part of the table type that says which columns identify a row. You write it with WITH KEY or WITH UNIQUE KEY after the kind, for example TYPE SORTED TABLE OF ty_item WITH UNIQUE KEY claim_id item_no. A unique key means two rows can never have the same key values. When INSERT meets a duplicate it adds nothing and sets sy-subrc to 4; when an assignment would copy duplicate keys into a table with a unique key, the program stops with a runtime error that cannot be caught. For hashed tables the key must be unique. Standard tables may use a non-unique key, and they can also be declared WITH EMPTY KEY when you never look rows up by key and only loop over them, as in the first example.

A table whose type names no key gets the standard key, which is made of all its character-like and byte-like columns; WITH DEFAULT KEY asks for it explicitly. It is easy to misuse, because it silently includes columns you did not intend, so new code names its key or uses an empty key.

Adding, changing and removing rows

Six statements cover most of what you do to a table. Each works on a work area, a single structure that holds one row on its way in or out, or on an inline row built with VALUE.

  • APPEND adds a row at the end. It suits standard tables. On a sorted table it is allowed only when the new row keeps the order and the key unique; on a hashed table it is not allowed.

  • INSERT adds a row. On a standard table it appends or, with INDEX, inserts at a position. On a sorted table it puts the row where the key belongs. On a hashed table it adds the row by key. When the key already exists in a unique table, INSERT does not add the row and sets sy-subrc to 4.

  • MODIFY changes a row, found by index or by key, to the values of the work area, and MODIFY ... TRANSPORTING limits the change to named columns. The key columns of the primary key of a sorted or hashed table are read-only: changing one stops the program with a runtime error that cannot be caught.

  • DELETE removes one row by index or key, or all rows that match a WHERE condition.

  • SORT rearranges a standard table by the columns you name, ascending by default, which is how a standard table gets an order after you filled it. A sorted table is already ordered and cannot be sorted with SORT. Name the columns after BY; without them the sort uses the primary key.

  • CLEAR removes all rows but keeps the memory the table has reserved, while FREE removes all rows and also releases that memory.

COLLECT is a special insert for tables whose rows consist of key columns plus numeric columns. It looks for a row with the same primary key: when one exists it adds the numeric values of the new row to it, and when none exists it inserts the row. Every column that is not part of the primary key must be numeric. Summing amounts per category is the classic use. The Keyword Documentation says COLLECT is not suitable for standard tables and recommends sorted or hashed tables for it, so the example below collects into a hashed table with a unique key.

The following example fills a standard table with APPEND and INSERT, changes and deletes rows, sorts, and then sums amounts per category into a hashed table with COLLECT. It requires an SAP system; not run here.

TYPES: BEGIN OF ty_item,
         claim_id TYPE c LENGTH 10,
         item_no  TYPE i,
         category TYPE c LENGTH 12,
         amount   TYPE p LENGTH 8 DECIMALS 2,
       END OF ty_item.
TYPES ty_items TYPE STANDARD TABLE OF ty_item WITH EMPTY KEY.

TYPES: BEGIN OF ty_total,
         category TYPE c LENGTH 12,
         amount   TYPE p LENGTH 8 DECIMALS 2,
       END OF ty_total.
TYPES ty_totals TYPE HASHED TABLE OF ty_total WITH UNIQUE KEY category.

DATA items  TYPE ty_items.
DATA totals TYPE ty_totals.

APPEND VALUE #( claim_id = 'C-1001' item_no = 1 category = 'TRAVEL' amount = '120.50' ) TO items.
APPEND VALUE #( claim_id = 'C-1001' item_no = 2 category = 'MEALS'  amount = '35.00' )  TO items.
INSERT VALUE #( claim_id = 'C-1001' item_no = 3 category = 'TRAVEL' amount = '60.00' )  INTO items INDEX 1.
" items now holds item 3, item 1, item 2 in this order

" change the amount of row 2 only
MODIFY items INDEX 2 FROM VALUE #( amount = '125.00' ) TRANSPORTING amount.

" remove every meals row
DELETE items WHERE category = 'MEALS'.

SORT items BY category amount DESCENDING.

LOOP AT items INTO DATA(item).
  COLLECT VALUE ty_total( category = item-category amount = item-amount ) INTO totals.
ENDLOOP.
" totals now holds one row: TRAVEL with amount 185.00 (125.00 + 60.00)

CLEAR items.   " rows gone, memory kept
FREE totals.   " rows gone, memory released

Header lines as legacy

Old ABAP programs declared tables with a header line: DATA itab TYPE TABLE OF ty_item WITH HEADER LINE, or the obsolete DATA ... OCCURS form. Such a table comes with a second data object of the same name, the header line, which serves as an implicit work area. The short forms of the statements that handle single rows, such as APPEND itab., then use that hidden row, and the name itab can mean the work area in one place and the table in another, so a reader cannot tell which one a statement touches without finding the declaration. Appending brackets, as in itab[], names the table itself.

The Keyword Documentation says the short forms that use a header line are allowed only outside ABAP Objects, and that OCCURS is forbidden in classes. Treat header lines as something to read in old programs and not to write in new code. When you change such code, replace the header line with a separate table and an explicit work area or an inline declaration such as LOOP AT itab INTO DATA(row).

Older material you may meet

Books and programs written before inline declarations and constructor expressions entered the language show a different style. The differences you will see most often are these.

  • Every variable was declared at the top in its own DATA statement; today you can declare at the point of first use with DATA(name) and FINAL(name).

  • Rows were built field by field in a work area, then appended with APPEND wa TO itab.; today VALUE builds the row or the whole table in one expression.

  • Tables often had header lines and OCCURS 0; the table kinds were rarely chosen on purpose, so nearly every table was a standard table searched with READ TABLE ... BINARY SEARCH after a manual SORT.

  • Single rows were read with READ TABLE and a work area; the table expression itab[ key = value ] now does the same inside a larger expression.

Code in the old style that already works does not have to be rewritten, but for new code choose a table kind to match the lookups you do, name the key and use the modern forms.

Lookup cost for the three kinds

How fast a read by key is depends on the table kind. On a standard table ABAP has no help: it compares the key with the rows one after the other until it finds a match, so the work grows in step with the number of rows, written . On a sorted table, a read by the key, or by the leading columns of it, can halve the remaining region at every step, so the work grows with the logarithm of the row count, written . On a hashed table a read by the full key goes straight to the row, and the work does not depend on the row count, written .

Table kind Order of rows Key Cost of a read by key

Standard

Order of adding

Unique or not

, rows are compared one by one

Sorted

Ordered by key

Unique or not

, by the key or its leading columns

Hashed

None

Must be unique

, by the complete key

These costs hold for the primary key. A read on a column that is not part of the key has to scan the rows on every kind of table; a second, secondary key can fix that, and it is covered in Table Expressions and Secondary Keys.

Example: the same lookup on all three kinds

The example fills three tables with the same claim items, one of each kind, and finds one item by its key in each. It uses READ TABLE ... WITH TABLE KEY, which is the form that lets the table use its own key: linear on the standard table, a binary search on the sorted table and a hash lookup on the hashed table. It also reads a row by index, which standard and sorted tables allow and hashed tables do not. It uses only statements that the local playground supports, so it can be tried there. It runs in the local playground and prints 35.00 with return code 0 for all three kinds, then C-1001 1 for the first sorted row.

TYPES: BEGIN OF ty_item,
         claim_id TYPE c LENGTH 10,
         item_no  TYPE i,
         amount   TYPE p LENGTH 8 DECIMALS 2,
       END OF ty_item.

TYPES ty_std    TYPE STANDARD TABLE OF ty_item WITH NON-UNIQUE KEY claim_id item_no.
TYPES ty_sorted TYPE SORTED   TABLE OF ty_item WITH UNIQUE KEY claim_id item_no.
TYPES ty_hashed TYPE HASHED   TABLE OF ty_item WITH UNIQUE KEY claim_id item_no.

DATA std    TYPE ty_std.
DATA sorted TYPE ty_sorted.
DATA hashed TYPE ty_hashed.
DATA found  TYPE ty_item.

DATA(rows) = VALUE ty_std(
  ( claim_id = 'C-1001' item_no = 1 amount = '120.50' )
  ( claim_id = 'C-1001' item_no = 2 amount = '35.00' )
  ( claim_id = 'C-1002' item_no = 1 amount = '80.00' ) ).

std    = rows.
sorted = rows.
hashed = rows.

" same key lookup, three different search strategies
READ TABLE std    INTO found WITH TABLE KEY claim_id = 'C-1001' item_no = 2.
WRITE / |standard: { found-amount } rc { sy-subrc }|.

READ TABLE sorted INTO found WITH TABLE KEY claim_id = 'C-1001' item_no = 2.
WRITE / |sorted:   { found-amount } rc { sy-subrc }|.

READ TABLE hashed INTO found WITH TABLE KEY claim_id = 'C-1001' item_no = 2.
WRITE / |hashed:   { found-amount } rc { sy-subrc }|.

" index access: standard and sorted tables only
READ TABLE sorted INTO found INDEX 1.
WRITE / |first sorted row: { found-claim_id } { found-item_no }|.

The assignment sorted = rows works even though rows is a standard table, because the rows are copied and inserted in key order; if two rows of rows had the same key, assigning to the unique sorted or hashed table would stop with a runtime error. For a table with a handful of rows the three kinds feel the same. The difference shows when the table holds tens of thousands of rows and the program reads it thousands of times.

Choosing a table kind

  • Use a standard table for a list that you fill once and read in order, or that you sort and loop over.

  • Use a sorted table when you need the rows in key order all the time, or when you read by the start of a key.

  • Use a hashed table for a lookup table that is read by its complete key many times, for example mapping a category code to its description.

  • Name the key of a table type on purpose; do not rely on the default key.

Summary

  • An internal table holds rows of one type in memory, and its table type fixes the row type, the table kind and the primary key.

  • Standard tables keep the order of adding, sorted tables keep key order, and hashed tables keep no order and need a unique key.

  • APPEND, INSERT, MODIFY, DELETE, SORT, CLEAR and FREE change a table; COLLECT sums numeric columns per key.

  • Header lines are legacy; do not use them in new code and expect to read them in old programs.

  • A read by key costs on a standard table, on a sorted table and on a hashed table, so choose the kind to match the lookups you do.

Try it

To do this by hand, follow the guide Work with Internal Tables: it builds claim items with VALUE, reads them with table expressions, sorts, groups and totals them.