Table expressions read internal-table rows directly in operand positions. This lecture explains key and index access, component chaining, line_exists, OPTIONAL, DEFAULT and the exception you must understand.
READ TABLE patterns with concise expressions without losing correct missing-row handling.1. Prepare an example table
TYPES: BEGIN OF ty_flight,
carrid TYPE c LENGTH 2,
connid TYPE c LENGTH 4,
price TYPE decfloat34,
END OF ty_flight,
ty_flights TYPE SORTED TABLE OF ty_flight
WITH UNIQUE KEY carrid connid.
DATA(lt_flights) = VALUE ty_flights(
( carrid = 'LH' connid = '0400' price = '199.00' )
( carrid = 'AA' connid = '0017' price = '650.00' ) ).
2. Read a row using a table expression
DATA(ls_flight) = lt_flights[
carrid = 'LH'
connid = '0400' ].
Square brackets replace the selection part of many READ TABLE statements. The result can be assigned, passed to a method or chained to a component.
DATA(lv_price) = lt_flights[
carrid = 'LH'
connid = '0400' ]-price.
DATA(ls_first) = lt_flights[ 1 ].
3. Missing rows and CX_SY_ITAB_LINE_NOT_FOUND
If a plain table expression cannot find a row, it normally raises CX_SY_ITAB_LINE_NOT_FOUND. It does not simply set sy-subrc like classic READ TABLE.
TRY.
DATA(ls_unknown) = lt_flights[ carrid = 'XX' ].
CATCH cx_sy_itab_line_not_found.
" Handle the missing row
ENDTRY.
Choose the handling style according to your intent: an exception for an unexpected missing row, line_exists for a Boolean check, or a default value when absence is normal.
4. line_exists — check without reading the row
IF line_exists( lt_flights[
carrid = 'LH'
connid = '0400' ] ).
" Row exists
ENDIF.
Use line_exists when only existence matters. If the next step also needs the complete row, avoid an unnecessary duplicate lookup—perform one suitable read instead.
5. OPTIONAL — return an initial value
Wrap a table expression with VALUE and add OPTIONAL when a missing row should produce an initial value instead of an exception.
DATA(ls_safe) = VALUE #(
lt_flights[ carrid = 'XX' ] OPTIONAL ).
IF ls_safe IS INITIAL.
" No matching row
ENDIF.
6. DEFAULT — return a meaningful fallback
DATA(ls_result) = VALUE #(
lt_flights[ carrid = 'XX' ]
DEFAULT VALUE #(
carrid = 'NA'
connid = '0000'
price = 0 ) ).
7. ASSIGNING-style access
A table expression can be used in an ASSIGN statement to work with the original row through a field symbol.
ASSIGN lt_flights[
carrid = 'LH'
connid = '0400' ] TO FIELD-SYMBOL(<ls_flight>).
IF sy-subrc = 0.
<ls_flight>-price = '209.00'.
ENDIF.
8. Key choice and performance
- Match the access pattern to a suitable primary or secondary key.
- Do not assume shorter syntax changes the table's key behavior.
- Avoid reading the same line repeatedly inside one statement or loop.
- Measure realistic hot paths; readability and correct key design come first.
- Use an index expression only when the row order is part of the program logic.
9. Classic READ TABLE vs expression
| Need | Good choice |
|---|---|
| Use row and handle via sy-subrc | READ TABLE or ASSIGN |
| Direct value in an expression | Table expression |
| Existence only | line_exists |
| Missing row is normal | OPTIONAL or DEFAULT |
10. Common mistakes
- Expecting
sy-subrcafter a plain table expression. - Ignoring
CX_SY_ITAB_LINE_NOT_FOUND. - Calling
line_existsand then repeating the same expensive lookup for the row. - Using an initial structure from
OPTIONALwhen an initial row could also be a valid business value. - Using a standard table with an unsuitable key for frequent keyed access.
11. Practice exercises
- Read a row by composite key and return one component.
- Check whether a carrier exists with
line_exists. - Return an initial row with
OPTIONAL. - Return a custom fallback structure with
DEFAULT. - Rewrite a
READ TABLE ... ASSIGNINGblock with a table expression and field symbol. - Explain which variant you would use when a missing row indicates a programming error.
12. Interview quick checks
Does a missing table-expression row set sy-subrc?
A plain table expression normally raises CX_SY_ITAB_LINE_NOT_FOUND. Some surrounding statements, such as ASSIGN, have their own handling behavior.
What is the difference between OPTIONAL and DEFAULT?
OPTIONAL returns an initial result of the required type. DEFAULT returns the explicitly supplied fallback.
Should line_exists be followed by another identical table expression?
Not automatically. If you need the row as well as the check, one read is often clearer and avoids duplicate selection.
Official SAP references
- Table expressions — ABAP Keyword Documentation
- OPTIONAL and DEFAULT — SAP Help
- Table expression chainings — SAP Help
Examples are educational. Verify syntax availability, table keys and released APIs in the ABAP Keyword Documentation for your target system.
0 Comments