ABAP New Syntax Lecture 3: Internal Table Expressions, LINE_EXISTS and OPTIONAL

ABAP New Syntax Lecture 3 Internal Table Expressions

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.

Learning outcome: You will replace suitable 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 ].
ABAP table expressions key and index examples

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 ) ).
ABAP line_exists OPTIONAL and DEFAULT safe table reads

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

NeedGood choice
Use row and handle via sy-subrcREAD TABLE or ASSIGN
Direct value in an expressionTable expression
Existence onlyline_exists
Missing row is normalOPTIONAL or DEFAULT

10. Common mistakes

  • Expecting sy-subrc after a plain table expression.
  • Ignoring CX_SY_ITAB_LINE_NOT_FOUND.
  • Calling line_exists and then repeating the same expensive lookup for the row.
  • Using an initial structure from OPTIONAL when 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

  1. Read a row by composite key and return one component.
  2. Check whether a carrier exists with line_exists.
  3. Return an initial row with OPTIONAL.
  4. Return a custom fallback structure with DEFAULT.
  5. Rewrite a READ TABLE ... ASSIGNING block with a table expression and field symbol.
  6. 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

Examples are educational. Verify syntax availability, table keys and released APIs in the ABAP Keyword Documentation for your target system.

Post a Comment

0 Comments