Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 36 ยท ABAP Basics

Data Types, Structures and Internal Tables

Master ABAP structures and internal tables: standard, sorted and hashed tables, LOOP AT, READ TABLE, APPEND, SORT, table expressions and FOR loops.

Business data comes in rows: order items, customers, invoice lines. In ABAP, the in-memory container for rows is the internal table. If you learn only one data structure in ABAP, this is it. Nearly every report fills an internal table (usually from the database), processes it and prints or returns it.

ABAP cannot run on this site, so ABAP listings are read-only and the runnable blocks are JavaScript stand-ins with the same ideas. Verify ABAP in a real system.

Structures

A structure groups fields of different types into one record, like a row. You define it locally or, better, reuse a dictionary structure from SE11.

TEXT
TYPES: BEGIN OF ty_item,
         material TYPE matnr,
         qty      TYPE i,
         price    TYPE p LENGTH 8 DECIMALS 2,
       END OF ty_item.

DATA ls_item TYPE ty_item.
ls_item-material = 'M-01'.
ls_item-qty      = 5.

You access a component with a hyphen: ls_item-qty. Naming habit: ls_ local structure, lt_ local table, lv_ local variable, gs_/gt_ global.

Internal tables

TEXT
DATA lt_items TYPE STANDARD TABLE OF ty_item WITH EMPTY KEY.

APPEND ls_item TO lt_items.
APPEND VALUE #( material = 'M-02' qty = 3 price = '4.50' ) TO lt_items.

An internal table has a line type and a table kind:

Kind Access Best for
STANDARD TABLE by index, or linear key search General lists, order matters
SORTED TABLE sorted by key; binary search Ordered data with frequent key reads
HASHED TABLE hash key, constant time Large lookup tables with unique keys

A standard table's key search walks the rows, so it slows down as the table grows. Sorted and hashed tables search fast but need a defined key (WITH UNIQUE KEY material).

Reading and changing rows

TEXT
LOOP AT lt_items INTO ls_item WHERE qty > 2.
  WRITE: / ls_item-material, ls_item-qty.
ENDLOOP.

READ TABLE lt_items INTO ls_item WITH KEY material = 'M-02'.
IF sy-subrc = 0.
  WRITE / 'found'.
ENDIF.

SORT lt_items BY qty DESCENDING.
DELETE lt_items WHERE qty = 0.
DELETE ADJACENT DUPLICATES FROM lt_items COMPARING material.
DESCRIBE TABLE lt_items LINES DATA(lv_lines).  " or lines( lt_items )

sy-tabix holds the current row number in a loop. LOOP AT ... ASSIGNING FIELD-SYMBOL(<ls>) gives a reference to the row instead of a copy, so changes apply directly and large rows are not copied: it is faster for big tables.

Modern syntax (ABAP 7.40 and later)

TEXT
DATA(ls_hit)  = lt_items[ material = 'M-02' ].     " table expression, raises an exception if missing
DATA(lv_qty)  = VALUE #( lt_items[ material = 'X' ]-qty OPTIONAL ).
DATA(lt_big)  = FILTER #( lt_items WHERE qty > 2 ).   " needs a sorted/hashed key
DATA(lt_names) = VALUE string_table( FOR ls IN lt_items ( ls-material ) ).
DATA(lv_total) = REDUCE i( INIT s = 0 FOR l IN lt_items NEXT s = s + l-qty ).

VALUE #( ... ) builds a value, FOR loops inside an expression and REDUCE folds a table into one value.

Same ideas, runnable

The stand-in below mirrors the pieces: append, loop with a condition, read with a key, sort, and the reduce idea.

JS Stand-in: internal table operations

Press Run (or Ctrl + Enter) to see the console output here.

No output.

For lookups in a big table, key access matters. The stand-in compares a linear find to a Map, the idea behind a hashed table.

JS Stand-in: linear versus hashed access

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Header lines and obsolete forms

Very old code uses tables with header line (DATA itab TYPE TABLE OF mara WITH HEADER LINE), where the table name also refers to a hidden work area. It confuses readers, is forbidden in classes and should never be used in new code. The same applies to OCCURS and TABLES statements. When you maintain old reports, read them carefully, and use the structure-based style shown above for anything new. The next lesson shows how rows arrive in such tables from the database: Open SQL: Reading the Database.

Common mistakes

  • Reading a standard table inside a loop with READ TABLE for every row: quadratic runtime on big data. Use a sorted/hashed table or BINARY SEARCH after SORT.
  • Forgetting CLEAR on the work area in a loop, which carries old values forward.
  • Using a table expression itab[ ... ] without handling the exception for missing rows.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which table kind gives constant-time access by a unique key?

    Hashed tables use a hash algorithm, so key access time does not grow with size.
  2. 2. What does sy-tabix hold inside a LOOP AT?

    sy-tabix is the row number of the current line.
  3. 3. Which statement adds a row at the end of a standard table?

    APPEND adds a row at the end. READ TABLE reads, SORT sorts.
  4. 4. Why use ASSIGNING FIELD-SYMBOL in a loop?

    A field symbol points at the row, so no copy is made and changes affect the table.

Summary

  • Structures group fields; internal tables hold many structured rows in memory.
  • Table kinds: standard, sorted (binary search) and hashed (unique key).
  • Use LOOP AT, READ TABLE, APPEND, SORT, DELETE and sy-subrc.
  • Modern expressions (VALUE, FOR, REDUCE, table expressions) shorten code.
  • Avoid header lines and repeated linear reads on big tables.

Next: Open SQL: Reading the Database