ERPClimb logoERPClimb

Code examples · Modern ABAP · Intermediate

ABAP internal tables: standard, sorted, hashed and secondary keys

The problem

A program holds 20,000 customers in memory and looks them up all the time, once by customer id and once by name.

How we solve it

  1. 1Declare the typesfour table kinds
  2. 2Fill them20,000 rows
  3. 3Read the same rowfour ways

The result

The same customer is found through four access paths.

Overview drawn by ERPClimb from this example. The steps are the same as in the code below.

1. The requirement

A program holds 20,000 customers in memory and looks them up all the time, once by customer id and once by name. A plain loop for every lookup is too slow. The right table type must make both lookups fast without copying the data twice.

2. How we design it

The access path decides the table type. A hashed table finds a row by its unique key in near constant time. A sorted table finds rows by a key prefix with a binary search. For a second access path on a standard table, a secondary key keeps a sorted index beside the data. We build all four and read the same customer from each.

Why we decided it this way

  1. 1Hashed table for lookup by unique idOnly key access is needed, and it is the fastest.
  2. 2Sorted table when order or partial keys matterReads in sorted order and by the first part of a key.
  3. 3Secondary key instead of a second tableOne copy of the data, two fast ways to find rows.

Before you start

  • ADT and a console class. No database table is used.

3. The code, step by step

Step 1 of 3: Declare four table types

One row type, four ways to store it.

ABAP · Class · ZCL_TABLE_KEYS_DEMO
" Row type and the four tables
CLASS zcl_table_keys_demo DEFINITION PUBLIC FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun.
ENDCLASS.

CLASS zcl_table_keys_demo IMPLEMENTATION.
  METHOD if_oo_adt_classrun~main.
    TYPES: BEGIN OF ty_cust,
             id   TYPE n LENGTH 8,
             name TYPE string,
           END OF ty_cust.

    DATA std     TYPE STANDARD TABLE OF ty_cust WITH EMPTY KEY.
    DATA hashed  TYPE HASHED TABLE OF ty_cust WITH UNIQUE KEY id.
    DATA sorted  TYPE SORTED TABLE OF ty_cust WITH NON-UNIQUE KEY name.
    DATA std_sec TYPE STANDARD TABLE OF ty_cust WITH EMPTY KEY
                      WITH NON-UNIQUE SORTED KEY by_name COMPONENTS name.
Part by part (plain text)
  1. Row type and the four tables. The standard table has no key of its own. The hashed table has a unique key on id. The sorted table is ordered by name. The last table is standard but also has a sorted secondary key called by_name, so we can search by name quickly without a second copy.

Open in the portal

The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.

4. The final outcome

The same customer is found through four access paths. You know which type to choose for which access.

What you should see

standard (loop): Customer 15000 hashed by id: Customer 15000 sorted by name: Customer 15000 secondary key: Customer 15000

Try it and check the result

  • Run with F9. All four lines show 'Customer 15000'.
  • Change the read for the hashed table to use name instead of id: it still activates and works, but the hash key is no longer used and every row is checked one by one. The fast access needs the full unique key.
  • To compare speed on your data volume, run the class in the ABAP profiler (ADT: Profile As, ABAP Application) and compare the four reads, instead of trusting a rule of thumb.

Common mistakes

  • A secondary key costs memory and time to build. Add one only if the table is read often by that field.
  • Inserting into a sorted table in the middle is slower than appending. If you only read once, a standard table plus a SORT may be cheaper.
  • READ TABLE ... BINARY SEARCH on a standard table is only correct if the table was sorted by the same fields first.
  • Do not guess performance. Measure with the profiler on realistic volumes.

Keep learning

Checking status: Reviewed line by line against SAP documentation. Not executed on a live system. Reviewed 2026-11-25. Release: ABAP 7.54+ / S/4HANA on-premise 2020+ and ABAP Environment on BTP. Check on your release.

Original ERPClimb teaching code with illustrative names. Not taken from a customer system. Syntax can differ by release; check the references below and activate in a sandbox first. ERPClimb is independent of SAP SE.