ERPClimb logoERPClimb
Internal Tables
ABAP Developmentbeginner 9 min read

Internal Tables Part 1: Standard, Sorted and Hashed Tables

Lessons are open to try during our launch period. Some advanced content may move to Pro later. AI practice limits apply separately.

Key takeaway

Understand internal tables from the ground up: what they are, the line type and the table key, the three table kinds (standard, sorted, hashed), work areas, and filling and reading a table from SFLIGHT in a classic report.

In this lessonShow sections

Concept visual

ABAP · Internal tablesERPClimb.com

Choosing standard, sorted and hashed tables

Standard

Index-based list

Good for ordered processing and append-heavy workloads; key lookup is normally linear unless using a secondary key.

Sorted

Ordered key

Maintains sort order; efficient key/range access using the sorted key.

Hashed

Hash key

Keyed lookups for unique full keys; no index-based traversal.

Rule of thumb

Pick the table kind based on how records will be read or changed, not solely on the number of records.

Performance varies with row count, key design and access patterns. Benchmark representative workloads rather than treating these as absolute runtime guarantees.

Visual overview

Internal Tables Part 1: Standard, Sorted and Hashed Tables — learning sheetFlow: Create report ZITAB_BASICS with the code example → Run it for airline LH; see the row count and the loop output → Change the READ key to a connection that does not exist and check sy-subrc = 4. Key ideas: WHAT AN INTERNAL TABLE IS; THE THREE PARTS OF A DEFINITION; THE THREE TABLE KINDS; CLASSIC DECLARATION WITH A WORK AREA. Watch out: Never use tables WITH HEADER LINE or OCCURS in new code — they are obsolete and confusing.ERPClimb.com / ABAP / LESSON 1/10 / LEARNING SHEETInternal Tables Part 1: Standard, Sorted and Hashed Tables1 · HOW IT FLOWS1Create reportZITAB_BASICS with thecode example2Run it for airline LH;see the row count and theloop output3Change the READ key to aconnection that does notexist and check sy-subrc…2 · KEY IDEAS EXPLAINEDWhat An Internal Table IsAlmost every ABAP program reads data from thedatabase into internal tables, processes it inmemory, and outputs or saves the result. Think of…The Three Parts Of A DefinitionLine type — usually a dictionary table/structure(SFLIGHT) or a local TYPES structure.Table kind — STANDARD, SORTED or HASHED.The Three Table KindsSTANDARD TABLE — rows in insertion order, accessedby index or by key with a linear search (slow onlarge tables unless sorted + BINARY SEARCH). The…SORTED TABLE — always kept sorted by its key; keyaccess uses a binary search automatically. Key canbe UNIQUE or NON-UNIQUE.Classic Declaration With A Work AreaTYPES: BEGIN OF ty_flight, carrid TYPE s_carr_id,connid TYPE s_conn_id, fldate TYPE s_date,seatsocc TYPE s_seatsocc, END OF ty_flight.3 · REMEMBER · WATCH OUTRememberAn internal table is a temporary, in-memory table in your program — itlives only while the program runs.It is defined by a line type (structure), a table kind (STANDARD,SORTED, HASHED) and a key.Classic processing uses a work area: read a row into it, change it,write it back.!Watch outNever use tables WITH HEADER LINE orOCCURS in new code — they areobsolete and confusing.4 · IN A REAL PROJECTA support developer changed a legacy report where an internal table was declared WITH HEADER LINE. He wroteCLEAR itab to empty the table, but that only cleared the header line; the table kept old rows and the next runof the loop produced duplicate records in an output file. The team fixed it with CLEAR itab[] and, in the next…Original ERPClimb learning sheet • Verify against your SAP release and edition

The situation: a real project scenario

A support developer changed a legacy report where an internal table was declared WITH HEADER LINE. He wrote CLEAR itab to empty the table, but that only cleared the header line; the table kept old rows and the next run of the loop produced duplicate records in an output file.
The team fixed it with CLEAR itab[] and, in the next change, replaced the header line with an explicit work area. They added "no header lines in new code" to their review checklist.

Explanation

An internal table is a temporary, in-memory table that exists only inside your program, living only as long as the program runs. It's defined by three things: a line type (a structure describing its columns), a table kind — STANDARD, SORTED, or HASHED — and a key.

Classic processing works through a work area: you read a row into it, change it, and write it back. One firm rule for new code: never use tables declared WITH HEADER LINE or OCCURS — they're obsolete and genuinely confusing.

This series covers internal tables in ten parts: this lesson on table kinds and the basics; Part 2 on choosing the right table kind; Part 3 on work area versus field symbol versus data reference; Part 4 on READ TABLE and sy-subrc; Part 5 on SORT, BINARY SEARCH, and secondary keys; Part 6 on APPEND, INSERT, MODIFY, DELETE, and COLLECT; Part 7 on removing duplicates; Part 8 on LOOP patterns and control levels; Part 9 on nested loops and the parallel cursor technique; and Part 10 on memory and large tables.

Almost every ABAP program follows the same basic shape: read data from the database into internal tables, process it in memory, then output or save the result. You can think of an internal table as an array of structures, where every row shares the same columns, defined by its line type.

A table's definition has three parts. The line type is usually a dictionary table or structure, such as SFLIGHT, or a local TYPES structure you define yourself. The table kind is one of STANDARD, SORTED, or HASHED. And the key is the set of fields that identifies a row, which can be declared UNIQUE or NON-UNIQUE.

The three table kinds behave quite differently from each other. A STANDARD TABLE keeps rows in insertion order and can be accessed by index or by key, though a key search is a linear scan — slow on large tables unless the table is sorted and read with BINARY SEARCH; it's the most common kind you'll see in classic code.

A SORTED TABLE is always kept sorted by its key, and key access automatically uses a binary search; its key can be UNIQUE or NON-UNIQUE.

A HASHED TABLE can only be accessed by its full unique key, via a hash function, giving constant access time regardless of how large the table grows — but it offers no index access at all.

A classic declaration with a work area looks like this: TYPES: BEGIN OF ty_flight, carrid TYPE s_carr_id, connid TYPE s_conn_id, fldate TYPE s_date, seatsocc TYPE s_seatsocc, END OF ty_flight. followed by DATA: gt_flights TYPE STANDARD TABLE OF ty_flight, gs_flight TYPE ty_flight. Here, the work area gs_flight holds exactly one row at a time.

To fill, read, and loop over the table: fill it from the database with SELECT ... INTO TABLE gt_flights; add a single row with APPEND gs_flight TO gt_flights; read one specific row with READ TABLE gt_flights INTO gs_flight WITH KEY connid = '0400'; process every row with LOOP AT gt_flights INTO gs_flight. ... ENDLOOP.; and count the rows with DESCRIBE TABLE gt_flights LINES lv_lines, or, in modern syntax, simply lines( gt_flights ).

On header lines specifically: older code sometimes declares DATA itab TYPE ... WITH HEADER LINE or OCCURS 0, which creates both the table and a work area sharing the same name. This is ambiguous — itab versus itab[] — and isn't even allowed inside classes. You'll need to be able to read this pattern in legacy code, but you should never write it yourself.

To practice: create report ZITAB_BASICS using the code example, run it for airline LH and observe the row count and loop output, then change the READ key to a connection number that doesn't exist and confirm that sy-subrc comes back as 4. Modern syntax, from release 7.40 onward, lets you write this more compactly: SELECT ... INTO TABLE @DATA(lt_flights), LOOP AT ... INTO DATA(ls_flight), and lines( ). With this you should be able to declare, fill, read, and loop over an internal table using a work area.

Code example

ABAP Code
REPORT zitab_basics. * ===== Step 1: line type, table and work area (classic style) =====TYPES: BEGIN OF ty_flight,         carrid   TYPE s_carr_id,         connid   TYPE s_conn_id,         fldate   TYPE s_date,         seatsmax TYPE s_seatsmax,         seatsocc TYPE s_seatsocc,       END OF ty_flight. DATA: gt_flights TYPE STANDARD TABLE OF ty_flight,        " standard table      gs_flight  TYPE ty_flight,                           " work area      gv_lines   TYPE i. PARAMETERS p_carr TYPE s_carr_id DEFAULT 'LH'. START-OF-SELECTION.* ===== Step 2: fill the table from the database =====  SELECT carrid connid fldate seatsmax seatsocc    FROM sflight    INTO TABLE gt_flights    WHERE carrid = p_carr.   IF sy-subrc <> 0.    WRITE: / 'No flights found for', p_carr.    STOP.  ENDIF.   DESCRIBE TABLE gt_flights LINES gv_lines.  WRITE: / 'Number of flights:', gv_lines. * ===== Step 3: read one row by key =====  READ TABLE gt_flights INTO gs_flight WITH KEY connid = '0400'.  IF sy-subrc = 0.    WRITE: / 'First flight of 0400 on', gs_flight-fldate.  ELSE.    WRITE: / 'Connection 0400 not in table'.  ENDIF. * ===== Step 4: add a row manually =====  CLEAR gs_flight.  gs_flight-carrid = p_carr.  gs_flight-connid = '9999'.  gs_flight-fldate = sy-datum.  APPEND gs_flight TO gt_flights. * ===== Step 5: process all rows =====  ULINE.  LOOP AT gt_flights INTO gs_flight.    WRITE: / sy-tabix, gs_flight-carrid, gs_flight-connid, gs_flight-fldate,             gs_flight-seatsocc, '/', gs_flight-seatsmax.  ENDLOOP. * ===== Other table kinds (declarations) =====DATA: gt_sorted TYPE SORTED TABLE OF ty_flight                WITH NON-UNIQUE KEY carrid connid fldate,      gt_hashed TYPE HASHED TABLE OF ty_flight                WITH UNIQUE KEY carrid connid fldate. * ===== OBSOLETE - only read it in legacy code =====* DATA itab TYPE ty_flight OCCURS 0 WITH HEADER LINE.

Watch out

Common mistakes

  • Declaring new tables WITH HEADER LINE or OCCURS.
  • Confusing itab and itab[] in legacy code.
  • Not checking sy-subrc after READ TABLE.
  • Forgetting CLEAR on the work area before filling a new row.
  • Using a standard table for large key lookups without sorting.

Do this

Best practices

  • Use explicit work areas (or field symbols) and no header lines.
  • Base line types on dictionary structures or clear local TYPES.
  • Choose the table kind based on access pattern (Part 2).
  • Always check sy-subrc after READ.
  • Keep tables local to the processing that needs them.

In the interview

Interview angle

Questions: "What is an internal table?", "Difference between standard, sorted and hashed tables?", "Why avoid header lines?"
Strong answer: an internal table is an in-memory table defined by line type, table kind and key; standard tables keep insertion order with index access and linear key search, sorted tables keep key order with automatic binary search, hashed tables allow only unique-key access with constant time; header lines are obsolete, ambiguous (itab vs itab[]) and not allowed in OO contexts.

Practice this lesson

Quick check

0 of 3 questions attempted

Question 1 of 3

Why should header lines (OCCURS 0 WITH HEADER LINE) be avoided?

Practise this in an interview

Answer in your own words and get instant feedback, the way an interviewer would judge it.

1What are the three types of internal tables in ABAP (Standard, Sorted, Hashed) and how do they differ in terms of key definition and access?

2What is the difference between STANDARD, SORTED, and HASHED internal tables?

Try a related Code Lab

This published exercise covers the same SAP topic. Apply the ideas in a hands-on scenario, then return to review your answers.

Open: READ TABLE without a key on a large standard table →

See it in code

Each example starts from a requirement, shows the design, then the code step by step with a plain-language explanation of every part.

Was this lesson helpful?

Keep exploring

Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.