ITAB Types in ABAP #
Itab type table of #
Objectives #
- Understand what an
INTERNAL TABLEis in ABAP - Declare a structure type with TYPES
- Create a structure type variable for rows
- Create an
INTERNAL TABLEtype variable - Fill an
INTERNAL TABLEwith structured data - Assimilate the analogy between
INTERNAL TABLEand address book - Apply best practices for manipulating internal tables
Definition #
An
INTERNAL TABLEis a temporary data container in an ABAP program.
It allows storing multiple rows of structured data, where each row can be aSTRUCTUREor a simple type.
Imagine an
INTERNAL TABLEas an address book
- Each page corresponds to a table row (a
STRUCTURE)- Each field on the page corresponds to a field of the
STRUCTURE(name, first name, phone…)- You can add or remove pages without affecting the others
INTERNAL TABLESare only in memory during program execution, they are not persistent like a database table.
Declaration #
1️⃣ Define a structure type for a row:
TYPES: BEGIN OF ty_person,
nom TYPE char20,
prenom TYPE char20,
age TYPE i,
ville TYPE char20,
END OF ty_person.
2️⃣ Declare the INTERNAL TABLE:
DATA: lt_persons TYPE TABLE OF ty_person.
3️⃣ Declare a temporary structure to add rows:
DATA: ls_person TYPE ty_person.
" or alternatively
DATA: ls_person_2 LIKE LINE OF lt_persons.
The TYPES
ty_person= identity card
The TABLElt_persons= address book
The STRUCTUREls_person= blank page to fill before inserting
Value assignment and row insertion into a table #
ls_person-nom = 'Dupont'.
ls_person-prenom = 'Jean'.
ls_person-age = 35.
ls_person-ville = 'Paris'.
APPEND ls_person TO lt_persons.
We fill a card (
STRUCTURE) and paste it into the book (INTERNAL TABLE).
Always use a temporary
STRUCTUREto manipulate rows before inserting them into the table.
When you populate a table with multiple rows automatically (in a loop for example), don’t forget to CLEAR the structure after each row addition to the table (or before any value assignment).
Best practices #
| Best practice | Explanation |
|---|---|
| Define a structure for each row | Clarifies and standardizes data |
Use APPEND to add rows |
Maintains table order and integrity |
| Name tables and structures explicitly | Facilitates reading and maintenance |
| Manipulate rows via a temporary structure | Clear separation between row and table |
| CLEAR the structure before assignment or after addition | Avoids having erroneous data |
Exercises – Declaration and insertion #
1 – Create an internal table of books #
Declare an
INTERNAL TABLElt_livreswith aSTRUCTUREty_livrecontaining
- titre (CHAR30)
- auteur (CHAR30)
- annee (I)
- genre (CHAR20)
Add a row to the table.
SOLUTION
TYPES: BEGIN OF ty_livre,
titre TYPE char30,
auteur TYPE char30,
annee TYPE i,
genre TYPE char20,
END OF ty_livre.
DATA: lt_livres TYPE TABLE OF ty_livre,
ls_livre TYPE ty_livre.
ls_livre-titre = '1984'.
ls_livre-auteur = 'George Orwell'.
ls_livre-annee = 1949.
ls_livre-genre = 'Dystopie'.
APPEND ls_livre TO lt_livres.
2 – Add a second row #
Add a second book to the same table
lt_livres.
SOLUTION
ls_livre-titre = 'Le Petit Prince'.
ls_livre-auteur = 'Antoine de Saint-Exupéry'.
ls_livre-annee = 1943.
ls_livre-genre = 'Conte'.
APPEND ls_livre TO lt_livres.
3 – Create a customer table #
Declare an
INTERNAL TABLE lt_clientswith aSTRUCTURE ty_clientcontaining
- nom (CHAR20)
- prenom (CHAR20)
- age (I)
- ville (CHAR20)
Add two different customers.
SOLUTION
TYPES: BEGIN OF ty_client,
nom TYPE char20,
prenom TYPE char20,
age TYPE i,
ville TYPE char20,
END OF ty_client.
DATA: lt_clients TYPE TABLE OF ty_client,
ls_client TYPE ty_client.
ls_client-nom = 'Martin'.
ls_client-prenom = 'Claire'.
ls_client-age = 28.
ls_client-ville = 'Lyon'.
APPEND ls_client TO lt_clients.
ls_client-nom = 'Bernard'.
ls_client-prenom = 'Paul'.
ls_client-age = 45.
ls_client-ville = 'Marseille'.
APPEND ls_client TO lt_clients.
Summary #
- An
INTERNAL TABLEcontains multiple rows of structured data.- Each row is a
STRUCTURE, manipulated via a temporaryVARIABLEbefore insertion.- General schema:
TYPES→DATA structure→DATA table→APPEND TO table.[!TIP] address book for the table, identity card for each row.
[!IMPORTANT] use clear
STRUCTURES, manipulate rows via a temporarySTRUCTUREand avoid header lines.
Itab type standard table of #
Objectives #
- Understand the difference between
TYPE TABLE OFandTYPE STANDARD TABLE OF - Know how to declare a standard internal table type
- Assimilate the specific behavior of a standard table (implicit index, insertion order)
- Identify relevant use cases
Definition #
A standard internal table (
TYPE STANDARD TABLE OF) is a table automatically indexed by ABAP.
Each row is accessible by a numeric index (1, 2, 3, …), representing the insertion order.
- Insertion order determines reading order.
- Access to a specific row often requires sequential search (
READ TABLEinstruction).- A standard table doesn’t enforce uniqueness on rows.
Internal table type Indexed Direct access Duplicates allowed Automatic sorting STANDARD TABLE ✅ Yes ❌ No ✅ Yes ❌ No SORTED TABLE ✅ Yes ✅ Yes (key) ❌ No (unique key) ✅ Yes HASHED TABLE ❌ No ✅ Yes (key) ❌ No (unique key) ❌ No
Declaration #
1️⃣ Define a row type #
TYPES: BEGIN OF ty_person,
nom TYPE char20,
prenom TYPE char20,
age TYPE i,
ville TYPE char20,
END OF ty_person.
2️⃣ Declare a standard table #
DATA: lt_persons TYPE STANDARD TABLE OF ty_person.
3️⃣ Declare a temporary structure #
DATA: ls_person TYPE ty_person.
ty_person→ identity card templatels_person→ card being filledlt_persons→ book of automatically numbered cards
Row insertion #
ls_person-nom = 'Dupont'.
ls_person-prenom = 'Jean'.
ls_person-age = 35.
ls_person-ville = 'Paris'.
APPEND ls_person TO lt_persons.
The
APPENDinstruction adds the row at the end of the table.
The standard table preserves chronological insertion order.
APPENDimplicitly creates a new index for the added row.
Data access #
Sequential reading #
- LOOP with storage of read row data in a structure already declared beforehand
LOOP AT lt_persons INTO ls_person.
WRITE: / ls_person-nom, ls_person-prenom.
ENDLOOP.
- LOOP with storage of read row data in a dynamically declared structure
LOOP AT lt_persons INTO DATA(ls_person).
WRITE: / ls_person-nom, ls_person-prenom.
ENDLOOP.
- LOOP with storage of read row data in a field-symbol already declared beforehand
LOOP AT lt_persons ASSIGNING <lfs_person>.
WRITE: / ls_person-nom, ls_person-prenom.
ENDLOOP.
- LOOP with storage of read row data in a dynamically declared field-symbol
LOOP AT lt_persons ASSIGNING FIELD-SYMBOL(<lfs_person>).
WRITE: / ls_person-nom, ls_person-prenom.
ENDLOOP.
Index reading #
READ TABLE lt_persons INDEX 2 INTO ls_person.
IF sy-subrc = 0.
WRITE: / 'Second person:', ls_person-nom.
ENDIF.
A standard table doesn’t guarantee sorting or direct access by key.
Access is done by index or sequential search.
Best practices #
| Best practice | Explanation |
|---|---|
Use TYPE STANDARD TABLE OF for simple lists |
Ideal for linear processing, without logical key |
| Define a clear type for rows | Simplifies maintenance and verification |
| Avoid repeated searches by key | Prefer SORTED TABLE for that |
Name objects (ty_, lt_, ls_) consistently |
Improves readability |
Standard tables → sequential manipulation (loops, appends, manual sorting).
Examples #
Example 1 – Book table #
TYPES: BEGIN OF ty_livre,
titre TYPE char30,
auteur TYPE char30,
annee TYPE i,
genre TYPE char20,
END OF ty_livre.
DATA: lt_livres TYPE STANDARD TABLE OF ty_livre,
ls_livre TYPE ty_livre.
ls_livre-titre = '1984'.
ls_livre-auteur = 'George Orwell'.
ls_livre-annee = 1949.
ls_livre-genre = 'Dystopie'.
APPEND ls_livre TO lt_livres.
ls_livre-titre = 'Le Petit Prince'.
ls_livre-auteur = 'Antoine de Saint-Exupéry'.
ls_livre-annee = 1943.
ls_livre-genre = 'Conte'.
APPEND ls_livre TO lt_livres.
Example 2 – Manual sorting of a standard table #
SORT lt_livres BY auteur.
A standard table is not automatically sorted.
SORTis necessary to organize rows by field.
Exercises #
1 – Customer table #
Declare a standard table
lt_clientswith a structurety_clientcontaining
- nom (CHAR20)
- prenom (CHAR20)
- age (I)
- ville (CHAR20)
Add two customers and display their names.
SOLUTION
TYPES: BEGIN OF ty_client,
nom TYPE char20,
prenom TYPE char20,
age TYPE i,
ville TYPE char20,
END OF ty_client.
DATA: lt_clients TYPE STANDARD TABLE OF ty_client,
ls_client TYPE ty_client.
ls_client-nom = 'Martin'.
ls_client-prenom = 'Claire'.
ls_client-age = 28.
ls_client-ville = 'Lyon'.
APPEND ls_client TO lt_clients.
ls_client-nom = 'Bernard'.
ls_client-prenom = 'Paul'.
ls_client-age = 45.
ls_client-ville = 'Marseille'.
APPEND ls_client TO lt_clients.
Summary #
TYPE STANDARD TABLE OF→ automatically indexed internal table.- Insertion order preserved.
- Sequential or index access.
- No sorting or unique key by default.
- Ideal for simple lists or linear processing.
Itab type sorted table of #
Objectives #
- Understand the structure and behavior of a sorted table (
TYPE SORTED TABLE OF) - Know how to declare a mandatory key with
WITH UNIQUE KEY - Identify performance advantages and constraints
- Master insertion, reading and automatic sorting
- Apply usage best practices
Definition #
A SORTED TABLE is an internal table automatically sorted according to a key defined during declaration.
Order is guaranteed and an internal index is maintained for key-based searches.
A directory sorted by name: each card is automatically inserted at the right position.
Impossible to have two cards with the same name if the key is unique.
- A key (
WITH UNIQUE KEYorWITH NON-UNIQUE KEY) is mandatory.- Insertion is automatically sorted.
- Searches are optimized (binary access).
- Massive insertions are more expensive than with a
STANDARD TABLE.
Declaration #
1️⃣ Define a row structure #
TYPES: BEGIN OF ty_person,
id TYPE char10,
nom TYPE char20,
age TYPE i,
END OF ty_person.
2️⃣ Declare a sorted table with unique key #
DATA: lt_persons TYPE SORTED TABLE OF ty_person
WITH UNIQUE KEY id.
3️⃣ Declare the work structure #
DATA: ls_person TYPE ty_person.
SORTED TABLEmaintains automatic sorted order.WITH UNIQUE KEYguarantees uniqueness on the key field.INSERTinserts the row at the right place, according to the key.
Row insertion #
ls_person-id = '0003'.
ls_person-nom = 'Martin'.
ls_person-age = 28.
INSERT ls_person INTO TABLE lt_persons.
ls_person-id = '0001'.
ls_person-nom = 'Dupont'.
ls_person-age = 45.
INSERT ls_person INTO TABLE lt_persons.
Regardless of code order, rows will be sorted by key (
id).
APPENDshould never be used in aSORTED TABLE.- Only the
INSERTinstruction maintains automatic sorting.- If a duplicate key is inserted → runtime error (
sy-subrc ≠ 0).
Best practices #
| Best practice | Explanation |
|---|---|
| Always define the key at declaration | Mandatory condition for sorting |
Use INSERT to maintain sorting |
APPEND breaks logical order |
Prefer SORTED TABLE for frequent reads |
Optimized key access |
| Avoid for massive disordered insertions | CPU cost of reordering |
A
SORTED TABLEis ideal when order and key-based search are priorities, not insertion speed.
Examples #
Example 1 – Product table sorted by id #
TYPES: BEGIN OF ty_produit,
id TYPE char10,
nom TYPE char20,
prix TYPE i,
END OF ty_produit.
DATA: lt_produits TYPE SORTED TABLE OF ty_produit
WITH UNIQUE KEY id,
ls_produit TYPE ty_produit.
ls_produit-id = '0002'.
ls_produit-nom = 'Stylo'.
ls_produit-prix = 2.
INSERT ls_produit INTO TABLE lt_produits.
ls_produit-id = '0001'.
ls_produit-nom = 'Cahier'.
ls_produit-prix = 5.
INSERT ls_produit INTO TABLE lt_produits.
Final order in the table is:
0001 Cahier,0002 Stylo.
Example 2 – Adding a third product #
ls_produit-id = '0003'.
ls_produit-nom = 'Gomme'.
ls_produit-prix = 1.
INSERT ls_produit INTO TABLE lt_produits.
Order remains automatically
0001,0002,0003.
Exercises #
1 – Create a customer table sorted by id #
Declare a table
lt_clientssorted byidand insert three disordered rows.
SOLUTION
TYPES: BEGIN OF ty_client,
id TYPE char10,
nom TYPE char20,
prenom TYPE char20,
END OF ty_client.
DATA: lt_clients TYPE SORTED TABLE OF ty_client
WITH UNIQUE KEY id,
ls_client TYPE ty_client.
ls_client-id = '003'.
ls_client-nom = 'Durand'.
ls_client-prenom = 'Alice'.
INSERT ls_client INTO TABLE lt_clients.
ls_client-id = '001'.
ls_client-nom = 'Martin'.
ls_client-prenom = 'Paul'.
INSERT ls_client INTO TABLE lt_clients.
ls_client-id = '002'.
ls_client-nom = 'Bernard'.
ls_client-prenom = 'Luc'.
INSERT ls_client INTO TABLE lt_clients.
LOOP AT lt_clients INTO ls_client.
WRITE: / ls_client-id, ls_client-nom.
ENDLOOP.
2 – Test a duplicate key #
Try to insert a row with an already existing
idand observe thesy-subrc.
SOLUTION
ls_client-id = '002'.
ls_client-nom = 'Dupont'.
ls_client-prenom = 'Jean'.
INSERT ls_client INTO TABLE lt_clients.
IF sy-subrc <> 0.
WRITE: / 'Error: key already exists.'.
ENDIF.
Summary #
TYPE SORTED TABLE OF→ internal table automatically sorted by key.- Mandatory key (
WITH UNIQUE KEYorWITH NON-UNIQUE KEY).- Very fast key access (binary).
- Slower insertion due to order maintenance.
- Forbidden to use
APPEND.- Ideal for sorted lists, fast searches and uniqueness guarantee.
Type range of #
Objectives #
- Understand the role of a
RANGE TABLE - Identify predefined fields:
SIGN,OPTION,LOW,HIGH - Understand the concept of inclusion/exclusion and intervals
Definition #
A
RANGE TABLEis a special internal table for defining value intervals.
It’s used to filter data inSQL queriesandselection screens.
Imagine an advanced search filter
- You indicate what you want to include or exclude
- You specify value intervals
- The RANGE table stores all this information in a structured way for the query
Predefined fields #
SIGN(C, 1) → Include (I) or Exclude (E)OPTION(C, 2) → operator: EQ, NE, GT, LT, BT…LOW→ lower limit of the intervalHIGH→ upper limit of the interval
Each row of the
RANGE TABLEcorresponds to a filtering rule.
Declaration #
" Declaration of a RANGE TABLE for product numbers (MATNR)
DATA: lr_matnr TYPE RANGE OF matnr.
The
lr_matnris directly usable inSELECTor forSELECT-OPTIONS.
Population #
DATA: ls_matnr TYPE LINE OF lr_matnr.
" Include products 1000 to 2000
ls_matnr-sign = 'I'.
ls_matnr-option = 'BT'.
ls_matnr-low = '1000'.
ls_matnr-high = '2000'.
APPEND ls_matnr TO lr_matnr.
" Exclude product 1500
ls_matnr-sign = 'E'.
ls_matnr-option = 'EQ'.
ls_matnr-low = '1500'.
ls_matnr-high = ''.
APPEND ls_matnr TO lr_matnr.
The
SIGN= I → inclusion, E → exclusion
TheOPTION= BT → between two values, EQ → single value
RANGE TABLESsimplify complex filtering without writing multipleIFconditions.
Exercises #
1 – Create a product range table #
Declare a
RANGE TABLElr_prodfor typematnr.
Add an included interval from 500 to 1000 and an exclusion for 750.
SOLUTION
DATA: lr_prod TYPE RANGE OF matnr,
ls_prod TYPE LINE OF lr_prod.
" Include 500 to 1000
ls_prod-sign = 'I'.
ls_prod-option = 'BT'.
ls_prod-low = '500'.
ls_prod-high = '1000'.
APPEND ls_prod TO lr_prod.
" Exclude 750
ls_prod-sign = 'E'.
ls_prod-option = 'EQ'.
ls_prod-low = '750'.
ls_prod-high = ''.
APPEND ls_prod TO lr_prod.
2 – Add a new interval #
Add an included interval from 1100 to 1200 and display the
RANGE TABLE.
SOLUTION
ls_prod-sign = 'I'.
ls_prod-option = 'BT'.
ls_prod-low = '1100'.
ls_prod-high = '1200'.
APPEND ls_prod TO lr_prod.
LOOP AT lr_prod INTO ls_prod.
WRITE: / 'SIGN:', ls_prod-sign,
'OPTION:', ls_prod-option,
'LOW:', ls_prod-low,
'HIGH:', ls_prod-high.
ENDLOOP.
3 – Add an exclusion #
Exclude product 1120 and display the entire
RANGE TABLE.
SOLUTION
ls_prod-sign = 'E'.
ls_prod-option = 'EQ'.
ls_prod-low = '1120'.
ls_prod-high = ''.
APPEND ls_prod TO lr_prod.
LOOP AT lr_prod INTO ls_prod.
WRITE: / 'SIGN:', ls_prod-sign,
'OPTION:', ls_prod-option,
'LOW:', ls_prod-low,
'HIGH:', ls_prod-high.
ENDLOOP.
Row order doesn’t matter. Each row is evaluated during filtering.
Summary #
TYPE RANGE OF→ internal table to store intervals and exclusions.- Fields:
SIGN,OPTION,LOW,HIGH- Analogy: an advanced filter that memorizes inclusions, exclusions and intervals
- Tip: each
APPENDadds a new filtering “rule”
Type hashed table #
Objectives #
- Understand how a
HASHED TABLEworks - Identify differences with a
SORTED TABLE - Know how to correctly declare the mandatory unique key (
WITH UNIQUE KEY) - Know how to insert and directly read rows via the key
Definition #
A
HASHED TABLEis an internal table with direct access by unique key.
It uses a hash calculation to locate each row instantly.
Unlike theSORTED TABLE, there is no order or implicit index.
Imagine a locker with unique code in a cloakroom
- Each code (key) directly opens the right locker (row).
- No need to browse other lockers to find the right one.
- Two identical keys cannot coexist – the key must be unique.
- The unique key is mandatory (
WITH UNIQUE KEY).- Row order is never guaranteed.
- Massive insertions and key-based reads are very efficient.
Declaration #
1️⃣ Define a structure type for rows:
TYPES: BEGIN OF ty_tab,
obj1 TYPE char10,
obj2 TYPE char20,
obj3 TYPE i,
END OF ty_tab.
2️⃣ Declare the HASHED internal table:
DATA: lt_tab_types TYPE HASHED TABLE OF ty_tab
WITH UNIQUE KEY obj1.
WITH UNIQUE KEY obj1→ mandatory primary key.- No order, no explicit index. The key identifies each row.
Insertion and key-based reading #
DATA: ls_tab TYPE ty_tab.
" Insertion
ls_tab-obj1 = '0001'.
ls_tab-obj2 = 'Dupont'.
ls_tab-obj3 = 45.
INSERT ls_tab INTO TABLE lt_tab_types.
IF sy-subrc = 0.
WRITE: / ls_tab-obj2, ls_tab-obj3.
ENDIF.
Here, the search is instant thanks to the hashing mechanism.
No loop or sorting is necessary to access a specific row.
Differences with sorted table #
| Criterion | Hashed table | Sorted table |
|---|---|---|
| Internal organization | Hash table (address calculation) | Table sorted by key |
| Index | No explicit index | Automatically created index |
| Key access | Direct (constant time) | Binary (logarithmic time) |
| Mandatory key | Yes, WITH UNIQUE KEY |
Yes, WITH UNIQUE KEY |
| Record order | Not guaranteed | Always sorted by key |
| Massive insertion | Very efficient | More expensive (reordering) |
- Sequential access (
LOOP AT) remains possible, but order is never guaranteed.- For frequent key-based searches,
HASHED TABLEis optimal.- Never insert two rows with the same unique key.
Exercises – Declaration and insertion #
1 – Create a hashed table of customers #
Declare an internal table
lt_clientswith a structurety_clientcontaining
- id (CHAR10) → unique key
- nom (CHAR20)
- ville (CHAR20)
Add at least two customers.
SOLUTION
TYPES: BEGIN OF ty_client,
id TYPE char10,
nom TYPE char20,
ville TYPE char20,
END OF ty_client.
DATA: lt_clients TYPE HASHED TABLE OF ty_client
WITH UNIQUE KEY id,
ls_client TYPE ty_client.
ls_client-id = 'C002'.
ls_client-nom = 'Martin'.
ls_client-ville = 'Paris'.
INSERT ls_client INTO TABLE lt_clients.
ls_client-id = 'C001'.
ls_client-nom = 'Dupont'.
ls_client-ville = 'Lyon'.
INSERT ls_client INTO TABLE lt_clients.
Summary #
HASHED TABLE→ internal table with direct access by unique key.- Mandatory:
WITH UNIQUE KEY.- Advantage: very fast key-based reading, efficient insertion.
- Disadvantage: no natural order, no guaranteed ordered sequential access.
[!IMPORTANT] clearly define the key, avoid duplicates, favor key-based searches.