Code examples · Modern ABAP · Beginner
Internal table basics: add, find, change, delete and sort rows
The problem
A trainee must learn how to hold a list of rows in memory.
How we solve it
- 1Definerow type and table
- 2Add rowsAPPEND, INSERT
- 3Find and changeREAD TABLE
- 4Delete and sortDELETE, SORT
- 5ShowLOOP
The result
Three items are left, sorted by quantity, and the program reports what it found.
1. The requirement
A trainee must learn how to hold a list of rows in memory. Create an item list, add rows at the end and in the middle, find one row by material, change its quantity, remove items with no quantity, and show the list biggest quantity first.
2. How we design it
An internal table is a list that lives only while the program runs. Its row layout is a structure type. We use a standard table, which keeps rows in the order you add them, and we use the modern VALUE constructor to build rows. Changing a row is done through a field symbol, which points at the row inside the table, so no copy has to be written back.
Why we decided it this way
- 1Standard tableIt keeps your insert order and is the right default for lists that are read from top to bottom.
- 2Field symbol to change a rowREAD TABLE ... ASSIGNING gives a pointer to the real row. Changing it changes the table. With INTO you only change a copy.
- 3Check sy-subrc after READ TABLEIf no row is found, sy-subrc is 4 and the work area is empty. Using it without the check is a classic beginner bug.
Before you start
- ADT (Eclipse) with a package such as $TMP, or transaction SE24 and SE38 in SAP GUI.
- No database data is needed. Everything is created in the program.
3. The code, step by step
Step 1 of 5: Define the row type and the table
A typed, empty list ready to receive rows.
" TYPES describes one row, DATA creates the table
CLASS zcl_itab_basics DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
ENDCLASS.
CLASS zcl_itab_basics IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
TYPES: BEGIN OF ty_item,
item TYPE i,
material TYPE string,
qty TYPE i,
END OF ty_item.
DATA items TYPE STANDARD TABLE OF ty_item WITH EMPTY KEY.
Part by part (plain text)
- TYPES describes one row, DATA creates the table. TYPES ... BEGIN OF describes what one row looks like: item number, material text and quantity. DATA ... TYPE STANDARD TABLE OF creates an empty table of such rows. WITH EMPTY KEY says we do not need a unique key, which is fine for a simple list.
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.
If it does not work
4. The final outcome
Three items are left, sorted by quantity, and the program reports what it found.
What you should see
Found PEN with quantity 5 Lines left: 3 15 INK 8 20 BOOK 6 10 PEN 5
Try it and check the result
- Put the five parts together in order, activate with Ctrl+F3 and run with F9 in ADT. The console shows the result above.
- Set a breakpoint on the READ TABLE statement and open the variable items in the debugger to see the rows.
- Change the key to 'PENCIL' and run again: nothing is printed for the first READ, because sy-subrc is 4.
Common mistakes
- Using READ TABLE ... INTO and then changing the work area does not change the table. Use ASSIGNING, or write the row back with MODIFY.
- A standard table searched with WITH KEY reads row by row. For big tables use a sorted or hashed table with a key, or a binary search.
- DELETE without WHERE or an index, inside a LOOP over the same table, can skip rows. Delete with WHERE, or use DELETE ... INDEX carefully.