Debugging
ABAP Developmentintermediate

Debugging Part 5: Debugging Internal Tables and Field Symbols

Key takeaway

Inspect and fix internal table problems in the debugger: table tool (content, filter, sort, row changes), READ TABLE issues (binary search on unsorted tables, default key), duplicate keys, header lines vs work areas, field symbols not assigned or assigned to the wrong row, and LOOP ... ASSIGNING changes.

Explanation

📌 KEY POINTS
✅ The debugger's table tool shows content, lets you filter/sort columns and compare tables (Diff tool) — faster than scrolling.

✅ Classic bugs: BINARY SEARCH on an unsorted table, READ TABLE without checking sy-subrc, work area not cleared, wrong table key.

✅ Field symbols: GETWA_NOT_ASSIGNED dumps if used before assignment; a field symbol assigned to a row changes that row directly.

⚠️ After READ TABLE fails, the work area still holds the previous row — using it without sy-subrc check gives silently wrong results.

━━━━━━━━━━━━━━━━━━━━
▶ WHERE WE ARE

Part 4 found value changes. Internal tables are where many ABAP bugs hide.

━━━━━━━━━━━━━━━━━━━━
▶ 1. TABLE TOOL

  • Double-click a table in the variables view → table tool.
  • Filter/sort columns, search values, show line count (LINES).
  • Change/insert/delete rows (test systems only).
  • Diff tool: compare two tables (e.g. before/after a routine).

━━━━━━━━━━━━━━━━━━━━
▶ 2. READ TABLE PROBLEMS

  • BINARY SEARCH requires the table sorted by exactly the search key fields; otherwise random misses.
  • READ TABLE … FROM wa uses the default key (for standard tables: all character-like fields) — use WITH KEY for clarity.
  • Always check sy-subrc; CLEAR the work area before reading if the result is used anyway.

━━━━━━━━━━━━━━━━━━━━
▶ 3. WORK AREAS AND HEADER LINES

  • Tables with header lines (old code): "itab" can mean the header or the body — confusing in debugging; the debugger shows both (itab[] vs itab).
  • Work areas not cleared between loop passes carry values into the next row.

━━━━━━━━━━━━━━━━━━━━
▶ 4. FIELD SYMBOLS

  • Check "assigned" status in the debugger; IS ASSIGNED in code.
  • LOOP … ASSIGNING <fs>: changes to <fs> change the table row directly (no MODIFY needed).
  • After the loop, <fs> still points to the last row — changing it later modifies that row unexpectedly.

━━━━━━━━━━━━━━━━━━━━
▶ 5. DUPLICATES AND SORTING

  • Duplicate keys after APPEND in loops → use SORT + DELETE ADJACENT DUPLICATES or sorted/hashed tables with unique keys.

━━━━━━━━━━━━━━━━━━━━
▶ STEP BY STEP

  1. Run the code example in the debugger and inspect the table after SORT.
  2. Remove the SORT and watch BINARY SEARCH miss.
  3. Check the field symbol's assignment after the loop.

✅ CHECKPOINT: you can diagnose internal table and field symbol bugs quickly.

Code example

ABAP Code
REPORT zdebug_itab. TYPES: BEGIN OF ty_mat,         matnr TYPE matnr,         werks TYPE werks_d,         stock TYPE labst,       END OF ty_mat. DATA: lt_mat TYPE STANDARD TABLE OF ty_mat,      ls_mat TYPE ty_mat. FIELD-SYMBOLS <ls_mat> TYPE ty_mat. START-OF-SELECTION.  ls_mat-matnr = 'M-03'. ls_mat-werks = '1000'. ls_mat-stock = 5.  APPEND ls_mat TO lt_mat.  ls_mat-matnr = 'M-01'. ls_mat-werks = '1000'. ls_mat-stock = 10. APPEND ls_mat TO lt_mat.  ls_mat-matnr = 'M-02'. ls_mat-werks = '1000'. ls_mat-stock = 0.  APPEND ls_mat TO lt_mat. * ===== Bug 1: BINARY SEARCH needs sorting by the search key =====  SORT lt_mat BY matnr werks.                  " remove this line to see random misses  CLEAR ls_mat.  READ TABLE lt_mat INTO ls_mat WITH KEY matnr = 'M-02' werks = '1000' BINARY SEARCH.  IF sy-subrc <> 0.                            " Bug 2: always check sy-subrc    WRITE: / 'M-02 not found'.  ELSE.    WRITE: / 'M-02 stock:', ls_mat-stock.  ENDIF. * ===== Field symbol: change rows directly =====  LOOP AT lt_mat ASSIGNING <ls_mat>.    IF <ls_mat>-stock = 0.      <ls_mat>-stock = 1.                      " row changed, no MODIFY needed    ENDIF.  ENDLOOP. * Bug 3: <ls_mat> still points to the LAST row here  UNASSIGN <ls_mat>.                           " avoid accidental changes later  IF <ls_mat> IS NOT ASSIGNED.    WRITE: / 'Field symbol released'.  ENDIF.

Real project scenario

A stock report showed correct numbers for most materials but zero for some. The table was sorted by MATNR only, while READ TABLE used BINARY SEARCH with MATNR and WERKS. For materials in several plants, the binary search randomly missed, and the code used the previous work area value because sy-subrc was not checked.
Sorting by both key fields and checking sy-subrc fixed it. The team later switched the lookup table to a sorted table with a unique key.

Common mistakes

  • BINARY SEARCH on differently sorted tables.
  • No sy-subrc check after READ TABLE.
  • Work areas not cleared.
  • Using field symbols after the loop.
  • Duplicate rows from repeated APPENDs.

Best practices

  • Use the table tool with filters and Diff.
  • Sort by exactly the search key or use sorted/hashed tables.
  • Check sy-subrc every time.
  • UNASSIGN field symbols after use.
  • Prefer explicit WITH KEY / WITH TABLE KEY.

Interview angle

Questions: "Why does READ TABLE BINARY SEARCH sometimes fail?", "What happens to a field symbol after LOOP ASSIGNING?", "How do you inspect tables in the debugger?"
Strong answer: binary search requires sorting by the exact key; check sy-subrc and clear work areas; ASSIGNING changes rows directly and stays assigned to the last row after the loop (UNASSIGN); table tool with filter/sort/Diff; default key pitfalls; duplicates.

Practice this lesson

Quick check

1. READ TABLE ... BINARY SEARCH returns sy-subrc 4 although the record exists. Most likely cause?

2. Which tool shows memory consumption of internal tables during a session?

3. You want to verify how the contents of an internal table changed between two points in a loop while debugging. Which debugger tool is best suited for this comparison?

Interview questions

1. Why is checking sy-subrc after every database operation and sy-tabix inside loops considered a debugging best practice, and what mistakes do junior developers commonly make around them?

Answer it in your own words first, then compare.

2. A READ TABLE does not return data even though you expect a record. What would you check in debugger?

Answer it in your own words first, then compare.

Practice more with Interview AI →

Reference-verified: the steps are checked against official SAP documentation and tutorials, and object and field names in the sample code are checked for consistency. The code has not been compiled or run on an SAP system by ERPClimb. Syntax marked release-dependent varies by SAP release, so check it in your own system.

More lessons in this topic

Interview questions on this topic

Related SAP topics