ABAP Guide: Work with 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.

Checked on 2026-11-13 against open-abap 2.14.30 (the abaplint transpiler and runtime, with open-abap-core commit a9acc41) and Node.js 24.21.

What you build

You write a class named zit_claim_items that holds the six items of one expense claim in an internal table, an internal table being a list of rows that lives only while the program runs. Four methods work on that list. build fills it with the VALUE expression, read_rows reads single rows in three ways, list_by_category sorts the table and prints the items under a heading per category, and total_and_filter adds up the amounts with REDUCE and keeps only the travel items with FILTER. A report named zit_internal_tables creates the class and calls the methods, and you run it on Node.js after every phase. At the end you ask for a row that does not exist and read the error that follows.

The playground is open-abap, a community open-source project, not SAP software. It runs only the part of the ABAP language that it implements, so a program that works here needs a check on a real SAP system before you rely on it there. The lab’s items are simpler than the real claim items of an SAP system: a category, an item number and a whole euro amount. The statement LOOP AT ... GROUP BY and the statement COLLECT do not run in the playground and are not used; they are explained on Internal Tables: Processing and Grouping.

Terminal with the command npm run run:items followed by the 17 output lines, from Claim 1001 has 6 items to Travel total: 89 EUR

Before you start

  • No account is needed: Node.js, the abaplint tools and open-abap install from the public npm registry without signing in. The licences you accept by installing (MIT for the abaplint packages) are listed in the README of the ABAP lab.

  • Set up the lab with Set Up a Local ABAP Playground and write the report of Your First ABAP Program once, so that you know the file layout (a .abap file and a .xml file per object) and the command that transpiles and runs a report. This guide adds files to the src folder of the same abap-lab folder.

  • The lab already holds the finished files of this guide in modules/ROOT/examples/abap-lab/src/. Type them yourself rather than copying them, so that you meet the errors in this guide on your own screen.

  • Read Internal Tables for the three table kinds and for what a key is. Two pages go deeper than this guide does: Table Expressions and Keys and Constructor Expressions.

  • The figures are drawings of the editor and the terminal, not screenshots. A highlighted box with a number marks the file or the output of the step with the same number.

Steps

The class grows by one method in each phase. In every phase you add two pieces to the class: the METHODS line in the DEFINITION part, which announces the method, and the METHOD ... ENDMETHOD block in the IMPLEMENTATION part, which holds its code. Then you add one line to the report that calls the method.

Write the class and build the table

  1. In the src folder, create the file src/zit_claim_items.clas.abap with the source below. The statement TYPES: BEGIN OF ty_item ... END OF ty_item describes one row, a structure with three components. TYPES ty_items TYPE STANDARD TABLE OF ty_item WITH EMPTY KEY describes the whole table: a standard table, which keeps its rows in the order they were added, with no key, because this guide only loops over the rows and looks them up with conditions. The method build hands the table back as its result. In it, VALUE #( ( ... ) ( ... ) ) builds a table: every pair of inner parentheses is one row, and # means that the type of the result is taken from the receiving variable items. The category stands first in the row, which matters in a later phase.

    CLASS zit_claim_items DEFINITION PUBLIC FINAL CREATE PUBLIC.
      PUBLIC SECTION.
        TYPES: BEGIN OF ty_item,
                 category TYPE string,
                 item_no  TYPE i,
                 amount   TYPE i,
               END OF ty_item.
        TYPES ty_items TYPE STANDARD TABLE OF ty_item WITH EMPTY KEY.
        METHODS build RETURNING VALUE(items) TYPE ty_items.
    ENDCLASS.
    
    CLASS zit_claim_items IMPLEMENTATION.
      METHOD build.
        items = VALUE #( ( category = `TRAVEL` item_no = 1 amount = 45 )
                         ( category = `MEALS`  item_no = 2 amount = 18 )
                         ( category = `TRAVEL` item_no = 3 amount = 32 )
                         ( category = `HOTEL`  item_no = 4 amount = 120 )
                         ( category = `MEALS`  item_no = 5 amount = 22 )
                         ( category = `TRAVEL` item_no = 6 amount = 12 ) ).
      ENDMETHOD.
    ENDCLASS.
  2. Create the attribute file src/zit_claim_items.clas.xml next to it:

    <?xml version="1.0" encoding="utf-8"?>
    <abapGit version="v1.0.0" serializer="LCL_OBJECT_CLAS" serializer_version="v1.0.0">
     <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
      <asx:values>
       <VSEOCLASS>
        <CLSNAME>ZIT_CLAIM_ITEMS</CLSNAME>
        <LANGU>E</LANGU>
        <DESCRIPT>Claim items</DESCRIPT>
        <STATE>1</STATE>
        <FIXPT>X</FIXPT>
        <UNICODE>X</UNICODE>
       </VSEOCLASS>
      </asx:values>
     </asx:abap>
    </abapGit>
    Visual Studio Code with the abap-lab project: the file src/zit_claim_items.clas.abap is open and shows the class zit_claim_items with the types ty_item and ty_items and the method build that fills the table with VALUE; callout 1 marks the class file and callout 2 marks the companion XML file in the explorer
  3. Create src/zit_internal_tables.prog.abap and its attribute file src/zit_internal_tables.prog.xml. The report creates the object, calls build, and writes how many rows the table has. lines( items ) is a built-in function that returns the number of rows.

    REPORT zit_internal_tables.
    
    START-OF-SELECTION.
      DATA(claim_items) = NEW zit_claim_items( ).
      DATA(items) = claim_items->build( ).
      WRITE / |Claim 1001 has { lines( items ) } items|.
    <?xml version="1.0" encoding="utf-8"?>
    <abapGit version="v1.0.0" serializer="LCL_OBJECT_PROG" serializer_version="v1.0.0">
     <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
      <asx:values>
       <PROGDIR>
        <NAME>ZIT_INTERNAL_TABLES</NAME>
        <SUBC>1</SUBC>
        <RLOAD>E</RLOAD>
        <UCCHECK>X</UCCHECK>
       </PROGDIR>
      </asx:values>
     </asx:abap>
    </abapGit>
    Visual Studio Code with the file src/zit_internal_tables.prog.abap open: a report named zit_internal_tables that creates the class, calls build and writes the number of items; callout 3 marks the report file in the explorer
  4. In the terminal, in the abap-lab folder, run the command that transpiles the sources and runs the report. Add this line to the scripts of package.json first, next to run:first: "run:items": "npm run transpile && node output/project/zit_internal_tables.prog.mjs". Because the command joins the two with &&, Node.js runs only when the transpile succeeded.

    npm run run:items
  5. Read the output. The transpiler prints a few lines about the files it found, and the last line comes from your report: the table holds six rows. The figure leaves out the transpiler lines.

    Terminal after npm run run:items: the program prints Claim 1001 has 6 items; callout 4 marks the command and callout 5 marks the output line

Read rows

A program often needs one row of a table, for example the item with number 3. ABAP has two ways to read one row: a table expression, which is the name of the table followed by a condition in square brackets, and the statement READ TABLE.

  1. In src/zit_claim_items.clas.abap, add the line that declares the method to the DEFINITION part, below the declaration of build, and the method itself to the IMPLEMENTATION part, below build. In the method, items[ item_no = 3 ] is a table expression: it stands for the first row whose item_no is 3, and DATA(third) declares a variable that receives that row. line_exists( items[ item_no = 9 ] ) is true or false, so nothing fails when the row is missing. READ TABLE ... WITH KEY copies a row into a variable and reports the result in sy-subrc: 0 when a row was found and 4 when it was not.

        METHODS read_rows IMPORTING items TYPE ty_items.
      METHOD read_rows.
        DATA(third) = items[ item_no = 3 ].
        WRITE / |Item 3: { third-category }, { third-amount } EUR|.
    
        IF line_exists( items[ item_no = 9 ] ).
          WRITE / `Item 9 exists`.
        ELSE.
          WRITE / `Item 9 does not exist`.
        ENDIF.
    
        READ TABLE items INTO DATA(found) WITH KEY category = `HOTEL`.
        WRITE / |READ TABLE: sy-subrc { sy-subrc }, item { found-item_no }|.
    
        READ TABLE items INTO found WITH KEY category = `FLIGHT`.
        WRITE / |READ TABLE: sy-subrc { sy-subrc }|.
      ENDMETHOD.
  2. In src/zit_internal_tables.prog.abap, add the call at the end of the report:

      claim_items->read_rows( items ).
  3. Run npm run run:items again.

  4. Read the four new lines. Item 3: TRAVEL, 32 EUR comes from the table expression. Item 9 does not exist comes from line_exists. The first READ TABLE looks for the category HOTEL, finds item 4 and sets sy-subrc to 0. The second looks for FLIGHT, finds nothing and sets sy-subrc to 4.

    Terminal after npm run run:items with the method read_rows: the program prints the count line, Item 3: TRAVEL, 32 EUR, Item 9 does not exist, READ TABLE with sy-subrc 0 and item 4, and READ TABLE with sy-subrc 4; callout 8 marks the command and callout 9 marks the four new lines

Sort and list by category

The items are in the order they were entered, and the categories are mixed. To print the items under one heading per category, sort the table by category first, then use a control-level statement inside a loop.

  1. Add the declaration of list_by_category and the method. DATA(sorted) = items copies the table, so that the original keeps its order. SORT sorted BY category item_no puts the rows in ascending order of category and, within a category, of item number. LOOP AT sorted INTO DATA(item) runs the block once per row. AT NEW category runs its block when the value of category differs from the row before, so it runs once per category and prints the heading. The statement compares the named column and every column to its left, which is why category is the first component of ty_item.

        METHODS list_by_category IMPORTING items TYPE ty_items.
      METHOD list_by_category.
        DATA(sorted) = items.
        SORT sorted BY category item_no.
    
        LOOP AT sorted INTO DATA(item).
          AT NEW category.
            WRITE / |{ item-category }:|.
          ENDAT.
          WRITE / |  item { item-item_no }, { item-amount } EUR|.
        ENDLOOP.
      ENDMETHOD.
  2. Add the call to the end of the report:

      claim_items->list_by_category( items ).
  3. Run npm run run:items again.

  4. Read the nine new lines. The categories come out in alphabetical order, HOTEL, MEALS and TRAVEL, and each heading appears once, followed by its items in item-number order. The reference page lists AT NEW among the older language elements: new code usually groups with LOOP AT ... GROUP BY, which needs a real ABAP system and is not run here.

    Terminal after npm run run:items with the method list_by_category: below the earlier lines the program prints the three category headings HOTEL, MEALS and TRAVEL, each followed by its items; callout 12 marks the command and callout 13 marks the nine new lines

Total and filter

  1. Add the declaration of total_and_filter and the method. REDUCE reduces a table to one value: INIT gives the start value of sum, FOR item IN items repeats over the rows, NEXT says what happens on every row, and the result is the final sum, of type i. FILTER with a WHERE condition on category builds a new table with only the matching rows. FILTER needs a table with a sorted or hashed key on the compared column, so the method first copies the items into by_category, a sorted table with the non-unique key category. A second REDUCE over the filtered table adds up the travel items.

        METHODS total_and_filter IMPORTING items TYPE ty_items.
      METHOD total_and_filter.
        DATA(total) = REDUCE i( INIT sum = 0
                                FOR item IN items
                                NEXT sum = sum + item-amount ).
        WRITE / |Total: { total } EUR|.
    
        DATA by_category TYPE SORTED TABLE OF ty_item WITH NON-UNIQUE KEY category.
        by_category = items.
        DATA(travel) = FILTER #( by_category WHERE category = `TRAVEL` ).
        WRITE / |Travel items: { lines( travel ) }|.
    
        DATA(travel_total) = REDUCE i( INIT sum = 0
                                       FOR item IN travel
                                       NEXT sum = sum + item-amount ).
        WRITE / |Travel total: { travel_total } EUR|.
      ENDMETHOD.
  2. Add the call to the end of the report:

      claim_items->total_and_filter( items ).
  3. Run npm run run:items again.

  4. Read the three new lines: the six items add up to 249 EUR (45 + 18 + 32 + 120 + 22 + 12), three of them are travel items, and the travel items add up to 89 EUR (45 + 32 + 12).

    Terminal after npm run run:items with the method total_and_filter: the last three lines are Total: 249 EUR, Travel items: 3 and Travel total: 89 EUR; callout 16 marks the command and callout 17 marks the three new lines

Read a row that is not there

A table expression stands for a row, so the row has to exist. You make that fail on purpose, in a place where you know the fix.

  1. In read_rows, change the first line of the method from items[ item_no = 3 ] to items[ item_no = 9 ]. There is no item 9. Save the file.

  2. Run npm run run:items. The report prints its first line, then the program stops: no row matches, so the table expression raises the exception cx_sy_itab_line_not_found, and nothing catches it. Node.js prints the exception as a stack trace. Read the lines that name your code: zit_claim_items.read_rows is the method, and the number pair after clas.mjs is the position in the generated JavaScript, not in your ABAP source. Node.js ends with exit code 1. The figure leaves out the unrelated lines of the trace and shortens the long paths with three dots.

    Terminal after npm run run:items when the table expression asks for item 9: the program prints Claim 1001 has 6 items and then Node.js prints a stack trace that names cx_sy_itab_line_not_found, the runtime function tableExpression and the method read_rows; callout 19 marks the exception line
  3. Change 9 back to 3, save and run npm run run:items. The 17 lines of output return.

Check the result

npm run run:items ends without an error and prints 17 lines: the count Claim 1001 has 6 items, the four lines of read_rows, the three headings with their six items, and the three lines Total: 249 EUR, Travel items: 3 and Travel total: 89 EUR. npm run lint prints abaplint: 0 issue(s) found, 14 file(s) analyzed and npm test still passes.

Variations

  • Catch the missing row instead of letting the program stop. Wrap the table expression for item 9 in TRY. and CATCH cx_sy_itab_line_not_found. and write a line in the CATCH part: the program then prints that line and goes on.

  • Give a fallback instead of an exception: VALUE ty_item( items[ item_no = 9 ] OPTIONAL ) returns an empty row, so amount is 0 and the program goes on.

  • Add a seventh item to build and run again to see the totals and the headings change. A new category such as FLIGHT gets its own heading, in alphabetical order.

  • Read a row by position: items[ 2 ] is the second row of the table in its current order.

What can go wrong

Symptom Cause Fix

Error: check_syntax, Method "read_row" not found, methodCallChain, zit_internal_tables.prog.abap:8 and no program output

The report calls a method name that the class does not have, here read_row instead of read_rows.

Open the file and line at the end of the message and correct the name. npm run lint shows the same finding with the column.

cx_sy_itab_line_not_found [Error] and a Node.js stack trace that names tableExpression

A table expression asked for a row that does not exist, and no TRY block catches the exception.

Ask first with line_exists( ), give a fallback with OPTIONAL, or catch the exception.

READ TABLE: sy-subrc 4 where you expected a row

No row matches the WITH KEY condition. READ TABLE does not stop the program: it only sets sy-subrc.

Check the key value, for example the spelling and the case of the category, and test sy-subrc before you use the variable.

Where to go next