SAP ABAP New Syntax: A Comprehensive Guide
Discover the modern features of SAP ABAP new syntax. Learn how to utilize inline declarations, string templates, the VALUE operator, and the FOR expression with detailed examples. Explore advanced topics like ABAP CDS and AMDP to enhance your SAP development skills.
SAP ABAP New Syntax: A Comprehensive Guide
SAP ABAP has long been a powerful and flexible programming language catering to the needs of enterprises. Over time, ABAP has evolved, incorporating new syntax structures to meet modern programming needs. In this blog post, we will introduce SAP ABAP's new syntax features and explain them with detailed examples.
What is ABAP New Syntax?
The new ABAP syntax aims to make the language more readable, easier to write, and compatible with modern programming approaches. It allows developers to accomplish more with less code and enhances performance.
Key New Syntax Features
1. New Data Declarations
The new syntax makes data declarations more concise and understandable. The DATA
statement can now define the type and value in a single line.
DATA(lv_value) = 5.
DATA(lv_text) = 'Hello World'.
2. Inline Declarations
Inline declarations automatically determine data types, making the code cleaner.
SELECT * INTO TABLE @DATA(lt_flights) FROM sflight WHERE carrid = 'LH'.
3. LOOP AT INTO DATA
Inline declarations can be used in loops to make the code more readable and concise.
LOOP AT lt_flights INTO DATA(ls_flight).
WRITE: / ls_flight-carrid, ls_flight-connid.
ENDLOOP.
4. String Templates
String concatenation is now easier and more readable.
DATA(lv_message) = |Flight number: { ls_flight-connid },
Carrier: { ls_flight-carrid }|.
WRITE: / lv_message.
5. VALUE Operator
The VALUE
operator allows easier filling of structures and internal tables.
DATA: lt_flights TYPE TABLE OF sflight,
ls_flight TYPE sflight.
ls_flight = VALUE sflight( carrid = 'LH' connid = '400' fldate = '20210701' ).
APPEND ls_flight TO lt_flights.
lt_flights = VALUE #( ( carrid = 'LH' connid = '401' fldate = '20210702' )
( carrid = 'LH' connid = '402' fldate = '20210703' ) ).
6. FOR Expression
The FOR
expression simplifies filling tables and structures.
DATA(lt_numbers) = VALUE #( FOR i = 1 THEN i + 1 UNTIL i > 10 ( i ) ).
DATA: lt_flights TYPE TABLE OF sflight.
lt_flights = VALUE #( FOR ls_flight IN lt_flights ( ls_flight )
WHERE ( carrid = 'LH' ) ).
Detailed Examples
Below are various ABAP code examples using the new syntax.
Example 1: Simple Data Processing
DATA: lt_flights TYPE TABLE OF sflight.
SELECT * INTO TABLE @lt_flights FROM sflight WHERE carrid = 'LH'.
LOOP AT lt_flights INTO DATA(ls_flight).
WRITE: / |Flight: { ls_flight-connid }, Carrier: { ls_flight-carrid },
Date: { ls_flight-fldate DATE = ISO }|.
ENDLOOP.
Example 2: Filling an Internal Table
DATA: lt_numbers TYPE TABLE OF i.
lt_numbers = VALUE #( FOR i = 1 THEN i + 1 UNTIL i > 10 ( i ) ).
LOOP AT lt_numbers INTO DATA(lv_number).
WRITE: / lv_number.
ENDLOOP.
Example 3: Structures and Nested Tables
TYPES: BEGIN OF ty_flight,
carrid TYPE s_carr_id,
connid TYPE s_conn_id,
fldate TYPE s_date,
END OF ty_flight.
DATA: lt_flights TYPE TABLE OF ty_flight.
lt_flights = VALUE #( ( carrid = 'LH' connid = '400' fldate = '20210701' )
( carrid = 'LH' connid = '401' fldate = '20210702' )
( carrid = 'LH' connid = '402' fldate = '20210703' ) ).
LOOP AT lt_flights INTO DATA(ls_flight).
WRITE: / |Carrier: { ls_flight-carrid }, Flight: { ls_flight-connid },
Date: { ls_flight-fldate DATE = ISO }|.
ENDLOOP.
Advanced Topics
1. ABAP CDS (Core Data Services)
ABAP CDS is a powerful tool for data modeling and access. With the new syntax, using CDS has become even easier.
DEFINE VIEW zcds_flight AS SELECT FROM sflight
{
key carrid,
key connid,
fldate
}
WHERE carrid = 'LH'.
2. AMDP (ABAP Managed Database Procedures)
AMDPs are used to perform database operations more efficiently by leveraging HANA SQLScript integrated with ABAP code.
CLASS zcl_amdp_example DEFINITION
PUBLIC
FINAL
CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES: if_amdp_marker_hdb.
METHODS: get_flights
IMPORTING VALUE(iv_carrid) TYPE s_carr_id
EXPORTING VALUE(et_flights) TYPE TABLE OF sflight.
ENDCLASS.
CLASS zcl_amdp_example IMPLEMENTATION.
METHOD get_flights BY DATABASE PROCEDURE FOR HDB
LANGUAGE SQLSCRIPT
OPTIONS READ-ONLY.
et_flights = SELECT * FROM sflight WHERE carrid = :iv_carrid;
ENDMETHOD.
ENDCLASS.
Conclusion
SAP ABAP's new syntax features provide developers with a more efficient and modern coding experience. In this article, we have explored the key new syntax features and provided example implementations. By leveraging the new syntax, you can develop ABAP projects faster and more effectively.