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.
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 sectionsHide sections
Concept visual
Choosing standard, sorted and hashed tables
Index-based list
Good for ordered processing and append-heavy workloads; key lookup is normally linear unless using a secondary key.
Ordered key
Maintains sort order; efficient key/range access using the sorted key.
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.
Visual overview
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
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?
Question 2 of 3
Which statement adds a row to a STANDARD table at the end?
Question 3 of 3
If an internal table type is declared simply as "DATA itab TYPE TABLE OF ty_line." without specifying STANDARD, SORTED, or HASHED, which table category is used by default?
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.