ABAP Dojo Guides Internal Tables

ABAP Internal Tables

Internal tables are the core data structure in ABAP — the equivalent of arrays or lists in other languages. They hold multiple rows of structured data in memory and can be processed with powerful built-in statements. This guide walks through the most common operations with runnable examples.

Declaring a Structured Type

Before creating a table, you define a structure type using TYPES: BEGIN OF ... END OF. This acts as a schema for each row in the table, specifying field names and their types. You can use this same structure type both for the table and for single-row work areas.

TYPES: BEGIN OF ty_employee,
         id    TYPE i,
         name  TYPE string,
         dept  TYPE string,
       END OF ty_employee.

Creating and Populating a Table

Declare a table variable using DATA ... TYPE TABLE OF, then populate it with APPEND. Each call to APPEND adds a new row at the end of the table. The work area (a single row variable) acts as a staging area before appending.

DATA: lt_employees TYPE TABLE OF ty_employee,
      ls_employee  TYPE ty_employee.

ls_employee-id   = 1.
ls_employee-name = 'Alice'.
ls_employee-dept = 'Finance'.
APPEND ls_employee TO lt_employees.

ls_employee-id   = 2.
ls_employee-name = 'Bob'.
ls_employee-dept = 'IT'.
APPEND ls_employee TO lt_employees.

Modern ABAP also supports VALUE syntax for compact inline initialization: lt_employees = VALUE #( ( id = 1 name = 'Alice' dept = 'Finance' ) ( id = 2 name = 'Bob' dept = 'IT' ) ).

Looping Through a Table

LOOP AT ... INTO iterates over every row, copying each row into the work area variable. This is the standard ABAP idiom for processing all rows. For better performance on large tables, prefer LOOP AT ... ASSIGNING FIELD-SYMBOL to avoid copying each row.

" Loop with INTO (copies each row)
LOOP AT lt_employees INTO ls_employee.
  WRITE: / ls_employee-id, ls_employee-name, ls_employee-dept.
ENDLOOP.

" Modern: loop with FIELD-SYMBOL (no copy, faster for large tables)
LOOP AT lt_employees ASSIGNING FIELD-SYMBOL(<ls_emp>).
  WRITE: / <ls_emp>-id, <ls_emp>-name.
ENDLOOP.

Counting Rows with LINES()

The LINES() function returns the number of rows in an internal table. It is the clearest way to check how many entries exist without looping. Use it in conditions, calculations, or WRITE statements to inspect table size at runtime.

DATA lv_count TYPE i.
lv_count = LINES( lt_employees ).
WRITE: / 'Number of employees:', lv_count.

Full Example

This complete example combines all the operations above: defining a structure, populating a table, looping through it, and printing the row count. Paste it into ABAP Dojo and click Run to see the output.

REPORT zinternal_tables.

TYPES: BEGIN OF ty_employee,
         id    TYPE i,
         name  TYPE string,
         dept  TYPE string,
       END OF ty_employee.

DATA: lt_employees TYPE TABLE OF ty_employee,
      ls_employee  TYPE ty_employee.

ls_employee-id   = 1.
ls_employee-name = 'Alice'.
ls_employee-dept = 'Finance'.
APPEND ls_employee TO lt_employees.

ls_employee-id   = 2.
ls_employee-name = 'Bob'.
ls_employee-dept = 'IT'.
APPEND ls_employee TO lt_employees.

ls_employee-id   = 3.
ls_employee-name = 'Carol'.
ls_employee-dept = 'HR'.
APPEND ls_employee TO lt_employees.

WRITE: / '=== Employee List ==='.
LOOP AT lt_employees INTO ls_employee.
  WRITE: / ls_employee-id, ls_employee-name, ls_employee-dept.
ENDLOOP.

WRITE: / '---'.
WRITE: / 'Total rows:', LINES( lt_employees ).
Try this code in ABAP Dojo →