SAP ABAP 7.40 New Syntax: 25 Practical Examples with Old vs New Code

SAP ABAP 7.40 new syntax 25 practical old vs new code examples
Quick answer: ABAP 7.40 introduced expression-oriented syntax that can make programs shorter and easier to read. This practical guide compares classic and modern ABAP using 25 copy-ready examples for inline declarations, Open SQL, table expressions, VALUE, FOR, REDUCE, COND, CORRESPONDING and more.

Compatibility note: ABAP 7.40 features arrived across different support packages. For example, several constructor expressions were expanded in SP05 and SP08. Always run the syntax check in your own system before using an example in production.

1. Inline DATA declaration

Declare the result exactly where it is first needed. This reduces boilerplate and keeps variable scope obvious.

" Classic
DATA ls_scarr TYPE scarr.
SELECT SINGLE * FROM scarr INTO ls_scarr WHERE carrid = 'LH'.

" ABAP 7.40
SELECT SINGLE * FROM scarr INTO @DATA(ls_scarr) WHERE carrid = 'LH'.

2. Inline field-symbol declaration

READ TABLE lt_scarr ASSIGNING FIELD-SYMBOL(<ls_scarr>)
  WITH KEY carrid = 'LH'.
IF sy-subrc = 0.
  <ls_scarr>-carrname = 'Lufthansa'.
ENDIF.

3. Modern Open SQL with host variables

Comma-separated fields and @ clearly separate ABAP variables from database columns.

DATA(lv_carrid) = 'LH'.
SELECT carrid, connid, cityfrom, cityto
  FROM spfli
  WHERE carrid = @lv_carrid
  INTO TABLE @DATA(lt_flights).

4. Read an internal table with a table expression

TRY.
    DATA(ls_carrier) = lt_scarr[ carrid = 'LH' ].
  CATCH cx_sy_itab_line_not_found.
    " Handle missing row
ENDTRY.

A direct table expression raises CX_SY_ITAB_LINE_NOT_FOUND when no row exists, so handle that case deliberately.

5. Check a row with line_exists

IF line_exists( lt_scarr[ carrid = 'LH' ] ).
  " The carrier exists
ENDIF.

6. Build a structure with VALUE

DATA(ls_scarr) = VALUE scarr(
  carrid   = 'LH'
  carrname = 'Lufthansa'
  currcode = 'EUR' ).

7. Build an internal table with VALUE

DATA(lt_ranges) = VALUE rsdsselopt_t(
  ( sign = 'I' option = 'EQ' low = '1000' )
  ( sign = 'I' option = 'BT' low = '2000' high = '2999' ) ).

8. Create an object with NEW

" Classic: CREATE OBJECT lo_service.
DATA(lo_service) = NEW lcl_service( ).

9. Convert a value with CONV

DATA(lv_text) = CONV string( lv_char_value ).
DATA(lv_amount) = CONV decfloat34( lv_input ).

10. Downcast with CAST

DATA(lo_subclass) = CAST lcl_subclass( lo_superclass ).
lo_subclass->special_method( ).

Use CAST only when the runtime object is compatible with the target type.

11. Conditional value with COND

DATA(lv_status) = COND string(
  WHEN lv_open = abap_true THEN 'Open'
  WHEN lv_closed = abap_true THEN 'Closed'
  ELSE 'Unknown' ).

12. Map one value with SWITCH

DATA(lv_text) = SWITCH string( lv_status
  WHEN 'A' THEN 'Active'
  WHEN 'I' THEN 'Inactive'
  ELSE 'Unknown' ).

13. Move same-named fields with CORRESPONDING

DATA(ls_target) = CORRESPONDING ty_target( ls_source ).

14. CORRESPONDING with MAPPING

DATA(ls_target) = CORRESPONDING ty_target(
  ls_source MAPPING customer = kunnr
                    company  = bukrs ).

15. Create rows with a FOR expression

DATA(lt_numbers) = VALUE int4_table(
  FOR n = 1 UNTIL n > 10
  ( n * 2 ) ).

16. Calculate a total with REDUCE

DATA(lv_total) = REDUCE decfloat34(
  INIT sum = CONV decfloat34( 0 )
  FOR item IN lt_items
  NEXT sum = sum + item-amount ).

17. Filter an internal table with FILTER

DATA(lt_open) = FILTER #( lt_orders WHERE status = 'OPEN' ).

FILTER requires a suitable sorted or hashed key for the filter condition. Otherwise, a normal loop may be clearer or necessary.

18. String templates and chaining

DATA(lv_message) = |Carrier { ls_scarr-carrid }: { ls_scarr-carrname }|.
DATA(lv_full) = |{ lv_first } { lv_last }| && | ({ lv_country })|.

19. Extend an existing value with BASE

lt_all = VALUE #( BASE lt_all
  ( id = 3 text = 'Third row' )
  ( id = 4 text = 'Fourth row' ) ).

20. Local helper values with LET

DATA(lv_result) = COND string(
  LET total = lv_net + lv_tax IN
  WHEN total > 1000 THEN 'High value'
  ELSE 'Standard' ).

21. Create a data reference with REF

READ TABLE lt_scarr INDEX 1 INTO DATA(ls_first).
IF sy-subrc = 0.
  DATA(lr_first) = REF #( ls_first ).
ENDIF.

22. Lossless conversion with EXACT

TRY.
    DATA(lv_number) = EXACT i( lv_decimal ).
  CATCH cx_sy_conversion_error.
    " Conversion would lose information
ENDTRY.

23. Group internal-table rows

LOOP AT lt_flights INTO DATA(ls_flight)
  GROUP BY ls_flight-carrid INTO DATA(lv_carrier).
  WRITE: / lv_carrier.
ENDLOOP.

24. Method chaining

DATA(lv_name) = lo_factory->get_service( )->get_customer( )->get_name( ).

Chaining is compact, but break it into steps when you need null checks, debugging points or clearer error handling.

25. Safe table expression with OPTIONAL

DATA(ls_carrier) = VALUE scarr(
  lt_scarr[ carrid = 'XX' ] OPTIONAL ).

If no matching row exists, the result is initial instead of raising CX_SY_ITAB_LINE_NOT_FOUND. Confirm availability in your support package.

Common mistakes to avoid

  • Do not replace every clear loop with a dense expression. Readability is the goal.
  • Do not ignore exceptions from table expressions, casts or exact conversions.
  • Do not assume every 7.40 system has the same support-package feature set.
  • Do not use inline variables so far from their declaration that their inferred type becomes unclear.
  • Do not expect new syntax alone to improve database performance. Efficient SQL and correct keys still matter most.

Performance and clean-code checklist

  • Select only required columns and rows; avoid SELECT * in production code.
  • Prefer set-based Open SQL over database calls inside loops.
  • Choose the correct internal-table category and key.
  • Use expressions when they clarify intent; use intermediate variables when they help debugging.
  • Measure with ST05, SAT or SQL Monitor instead of guessing.

ABAP 7.40 interview questions

  1. What is an inline declaration, and how is its type inferred?
  2. What happens when a table expression cannot find a row?
  3. When would you use COND instead of SWITCH?
  4. What is the difference between CONV, CAST and EXACT?
  5. How do VALUE and NEW differ?
  6. Why does modern Open SQL use @ for host variables?
  7. When can FILTER be used efficiently?
  8. What problems does CORRESPONDING ... MAPPING solve?

Practice exercises

  1. Convert a classic READ TABLE program into a safe table expression.
  2. Create a range table using one VALUE expression.
  3. Calculate order totals using REDUCE.
  4. Map a customer structure to a display structure using CORRESPONDING.
  5. Rewrite a nested IF block using COND or SWITCH, then compare readability.

Frequently asked questions

Is ABAP 7.40 new syntax mandatory?

No. Classic syntax remains valid in many systems. Use modern syntax when it improves clarity and your system release supports it.

Does new syntax make ABAP faster?

Usually it changes readability more than runtime. Database access patterns, algorithms, keys and data volume have a much larger performance impact.

Can I use these examples in SAP ECC?

Yes, if the ECC system runs a compatible AS ABAP 7.40 support package. Check the system release and activate a syntax check before transport.

What should I learn first?

Start with inline declarations, modern Open SQL, table expressions and VALUE. Then learn COND, CORRESPONDING, FOR and REDUCE.

Related SAP ABAP guides

Official SAP references

Final takeaway

Modern ABAP is most valuable when it makes business intent obvious. Learn the syntax, confirm your support package, keep error handling explicit and choose clarity over cleverness.

Post a Comment

0 Comments