ABAP Core
ABAP Developmentbeginner

ABAP Core Part 6: System Fields — sy-subrc, sy-tabix, sy-datum and More

Key takeaway

Use the most important system fields correctly: sy-subrc after statements, sy-tabix and sy-index in loops, sy-datum/sy-uzeit/sy-uname, sy-ucomm for user actions — and know when they are overwritten.

Explanation

📌 KEY POINTS
✅ sy-subrc = return code of the last statement (0 = success); check it immediately.

✅ sy-tabix = current row in LOOP AT / READ TABLE; sy-index = counter in DO/WHILE.

✅ sy-dbcnt = rows processed by the last SQL statement.

⚠️ Every statement can overwrite system fields — copy the value if you need it later.

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

Parts 1–5 used sy-subrc and sy-datum casually. This part explains the most important system fields and their traps.

━━━━━━━━━━━━━━━━━━━━
▶ 1. WHAT SYSTEM FIELDS ARE

The structure SY (type SYST) is filled by the ABAP runtime. You read it; you should never write to it (except a few documented cases).

━━━━━━━━━━━━━━━━━━━━
▶ 2. SY-SUBRC — THE RETURN CODE

Set by SELECT, READ TABLE, AUTHORITY-CHECK, CALL FUNCTION (with EXCEPTIONS), ASSIGN, and many more.

  • 0 = success; 4 = not found (typical); other values depend on the statement.
  • Check it in the next line — a following statement may change it.
  • Modern expressions (table expressions, method calls) often use exceptions instead of sy-subrc.

━━━━━━━━━━━━━━━━━━━━
▶ 3. SY-TABIX AND SY-INDEX

  • sy-tabix — index of the current row in LOOP AT (standard/sorted tables) and after READ TABLE.
  • sy-index — loop counter in DO and WHILE.
  • In nested loops, the values belong to the innermost loop.

━━━━━━━━━━━━━━━━━━━━
▶ 4. SY-DBCNT

Number of rows read/changed by the last SQL statement (e.g. after UPDATE, how many rows were updated).

━━━━━━━━━━━━━━━━━━━━
▶ 5. DATE, TIME, USER, LANGUAGE

  • sy-datum, sy-uzeit — application server date and time (server time zone!). For user-local time use sy-datlo/sy-timlo; for time stamps use GET TIME STAMP / utclong.
  • sy-uname — current user; sy-langu — logon language; sy-mandt — client.
  • In ABAP Cloud use cl_abap_context_info instead (see RAP module, ABAP Cloud).

━━━━━━━━━━━━━━━━━━━━
▶ 6. SY-UCOMM AND UI FIELDS

sy-ucomm holds the function code of the user action (e.g. in list or dynpro processing). In modern code, prefer the function code parameter of events (e.g. ALV user command) over reading sy-ucomm globally.

━━━━━━━━━━━━━━━━━━━━
▶ 7. SY-MSGID / SY-MSGNO / SY-MSGV1–4

After a function call with messages, these contain the message raised — useful for logging.

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

Run the code example: see sy-subrc after found/not found reads, sy-tabix in a loop, sy-dbcnt after SELECT, and how a WRITE statement does not reset sy-subrc but a READ does.

✅ CHECKPOINT: you use system fields safely and check them at the right moment.

Code example

ABAP Code
REPORT zabap_core_sysfields. DATA: lt_flights TYPE STANDARD TABLE OF sflight,      ls_f       TYPE sflight,      lv_rc      TYPE sy-subrc,      lv_ts      TYPE timestamp. START-OF-SELECTION.* ===== sy-subrc and sy-dbcnt after SQL =====  SELECT * FROM sflight INTO TABLE lt_flights    UP TO 10 ROWS    WHERE carrid = 'LH'.  WRITE: / 'SELECT sy-subrc:', sy-subrc, 'rows (sy-dbcnt):', sy-dbcnt. * ===== sy-subrc after READ TABLE: check IMMEDIATELY =====  READ TABLE lt_flights INTO ls_f WITH KEY connid = '9999'.  lv_rc = sy-subrc.                              " copy before other statements  IF lv_rc <> 0.    WRITE: / 'Connection 9999 not found, sy-subrc =', lv_rc.   " 4  ENDIF. * ===== sy-tabix in LOOP AT =====  LOOP AT lt_flights INTO ls_f.    WRITE: / 'row', sy-tabix, ls_f-connid, ls_f-fldate.  ENDLOOP. * ===== sy-index in DO =====  DO 3 TIMES.    WRITE: / 'DO iteration', sy-index.  ENDDO. * ===== date, time, user, client, language =====  WRITE: / 'server date/time:', sy-datum, sy-uzeit.  WRITE: / 'user-local date/time:', sy-datlo, sy-timlo.  WRITE: / 'user:', sy-uname, 'client:', sy-mandt, 'language:', sy-langu. * ===== time stamp (time zone independent) - better for persistence =====  GET TIME STAMP FIELD lv_ts.  WRITE: / 'UTC time stamp:', lv_ts. * ===== sy-msgid/msgno after a function call with messages =====*  CALL FUNCTION '...' EXCEPTIONS error_message = 1 OTHERS = 2.*  IF sy-subrc <> 0.*    MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno WITH sy-msgv1 sy-msgv2.*  ENDIF. * 💡 Modern syntax (7.40+): VALUE #( lt_flights[ 1 ] OPTIONAL ) reads a row without sy-subrc.

Real project scenario

A report checked sy-subrc of a READ TABLE, but a colleague later inserted a logging call between the READ and the IF. The logging function set sy-subrc to 0, so "not found" cases were processed as found, producing empty records in an interface file.
The fix copied sy-subrc into a variable immediately after the READ; in new code the team switched to table expressions with OPTIONAL/DEFAULT or line_exists( ), which avoid the problem entirely.

Common mistakes

  • Checking sy-subrc after other statements have overwritten it.
  • Confusing sy-tabix and sy-index.
  • Using sy-datum/sy-uzeit for user-visible local times across time zones.
  • Writing values into system fields.
  • Using system fields in ABAP Cloud code instead of released APIs.

Best practices

  • Check sy-subrc in the line right after the statement (or copy it).
  • Prefer expressions and exceptions in modern code (line_exists, OPTIONAL).
  • Use time stamps (UTC) for persistence.
  • Use sy-dbcnt to verify mass updates.
  • In ABAP Cloud, use cl_abap_context_info.

Interview angle

Questions: "What does sy-subrc = 4 mean after READ TABLE?", "Difference between sy-tabix and sy-index?", "Why copy sy-subrc?"
Strong answer: sy-subrc is the return code of the last statement (0 success, 4 typically not found); sy-tabix is the table row index in LOOP/READ, sy-index the DO/WHILE counter; sy-dbcnt counts SQL rows; system fields are overwritten by later statements, so check or copy them immediately; use user-local date/time or time stamps across time zones.

Practice this lesson

Quick check

1. After READ TABLE fails, which statement about the target work area is true?

2. When a SELECT SINGLE statement finds no matching record for its WHERE condition, what is the resulting state of the target work area and sy-subrc?

3. Which system field holds the return code of the last statement?

Interview questions

1. You're told that a report gives correct output in the foreground (SE38, F8) but wrong output when scheduled as a background job. What categories of causes would you investigate?

Answer it in your own words first, then compare.

2. A program did not clear the work area after a failed READ TABLE and processed stale data. How do you avoid this?

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