New ABAP 7.5 Syntax

Hello Dear ABAPers, If you want to learn ABAP 7.5 Syntax with new ABAP Syntax’s then here you will able to learn that with proper examples.

SAP’s ABAP 7.5 release introduced significant syntax improvements that modernize the language, making code more concise, readable, and aligned with contemporary programming paradigms. These enhancements help bridge the gap between traditional ABAP and modern programming languages while maintaining backward compatibility.

Inline Declarations

Inline declarations are a new way of declaring variables and field symbols at operand positions (i.e., at the
same position, where we are using them).

Inline declarations are possible only for

  • DATA &
  • FIELD-SYMBOLS
Old ABAP SyntaxNew ABAP Syntax
DATA: text TYPE string.
text = ‘demo’.
DATA(text) = ‘demo’.
TYPES: BEGIN OF ty_snwd_so,
node_key TYPE snwd_node_key,
so_id
TYPE snwd_so_id,
END OF ty_snwd_so.
DATA: t_snwd_so TYPE TABLE OF ty_snwd_so.
SELECT node_key
so_id
FROM snwd_so
UP TO 5 ROWS
INTO TABLE t_snwd_so.
SELECT node_key, so_id
FROM snwd_so
UP TO 5 ROWS
INTO TABLE @DATA(t_snwd_so).

Note: Internal table declaration – dynamically with
two columns/fields.

Inline declarations must be escaped in SELECT
using a ‘@’ escape character.
DATA: wa_snwd_so TYPE ty_snwd_so.
LOOP AT t_snwd_so INTO wa_snwd_so.
WRITE: / wa_snwd_so-node_key,
wa_snwd_so-so_id.
ENDLOOP.
LOOP AT t_snwd_so INTO DATA(wa_snwd_so).
WRITE: / wa_snwd_so-node_key,
wa_snwd_so-so_id.
ENDLOOP.
FIELD-SYMBOLS: TYPE ty_snwd_so.
LOOP AT t_snwd_so ASSIGNING .
IF IS ASSIGNED.
WRITE: / -node_key,
-so_id.
ENDIF.
ENDLOOP.
LOOP AT t_snwd_so ASSIGNING FIELD-
SYMBOL().
IF IS ASSIGNED.
WRITE: / -node_key,
-so_id.
ENDIF.
ENDLOOP.
FIELD-SYMBOLS: TYPE ty_snwd_so.
READ TABLE t_snwd_so ASSIGNING
READ TABLE t_snwd_so ASSIGNING FIELD-
SYMBOL() …
FIELD-SYMBOLS:<fs> TYPE ANY.
ASSIGN … TO <fs>.
ASSIGN … TO FIELD-SYMBOL(<fs>).

Escape Host Variables (SELECT)

Usually, Open SQL statements are executed on the database after being transformed to native SQL.
SQL expressions in many operand positions – the Open SQL parser must be able to distinguish clearly between
operands that are evaluated by the database and ABAP variables whose contents has to be passed to the
database.

ABAP variables in Open SQL statements plays the role of host variables – the ABAP host variables in Open
SQL must be prefixed / preceded with ‘@’.
In other words, Host variables – are variables declared in the ABAP program that are used in operand position
of Open SQL statements. In order to identify the host variables, the objects should always be prefixed by the
escape character ‘@’.

The escape character ‘@’ must be specified in the syntax check from Release 7.40 SP05.

Examples

Specifies various host variables; the escape character ‘@ is always used.

DATA carrid TYPE spfli-carrid.
cl_demo_input=>request( CHANGING field = carrid ).

DATA: BEGIN OF result,
carrid TYPE sflight-carrid,
connid TYPE sflight-connid,
END OF result.

SELECT carrid, connid
FROM sflight
INTO CORRESPONDING FIELDS OF @result
WHERE carrid = @carrid.

ENDSELECT.

DATA dref LIKE REF TO result.
SELECT carrid, connid
FROM sflight
INTO CORRESPONDING FIELDS OF @dref->*
WHERE carrid = @carrid.

ENDSELECT.

Chain Operations (Pipe Operator)

The purpose of a string template is to create a new character string out of literal texts and embedded
expressions. A string template is defined by using the ‘|’ (pipe) symbol at the beginning and end of a template. Embedded expressions are defined within a string template with curly brackets {expression}.

Note that a space between bracket and expression is obligatory.

In other words, the chaining operator can be used to create a character string out of multiple other strings and string templates.

Usually, we use CONCATENATE statement to join strings.

CONCATENATE lv_text1 lv_text2
INTO character_string
RESPECTING BLANKS.
DATA(character_string) =
|{ lv_text1 }{ lv_text2 }|.
Usually, CONCATENATE statement doesn’t respect
the spaces of the variables.

RESPECTING BLANKS keyword is used to respect the spaces of the string variables.
Pipe Operator respect all the spaces of the variables.

Examples

DATA: character_string TYPE string.
character_string = |This is a literal text.|.
character_string = |{ text–001 }|.

This example has in fact exactly the same result as:
character_string = ‘This is a literal text.’.

An expression can be a data object (variable), a functional method, a predefined function or a
calculation expression.
character_string = |{ numeric_variable }|.
character_string = |This resulted in return code { sy–subrc }|.

Using String Literals in SQL Query (Concatenated String)

String value can be concatenated , while fetching the values from the database – resulting in optimization and better performance.

TYPES: BEGIN OF ty_vbak,
vbeln TYPE vbeln_va,
netwr TYPE netwr_ak,
waerk TYPE waerk,
END OF ty_vbak.
“Sales Order
“Net Value
“Currency
DATA: t_vbak TYPE TABLE OF ty_vbak.
FIELD-SYMBOLS: TYPE ty_vbak.
SELECT vbeln
netwr
waerk
FROM vbak
UP TO 5 ROWS
INTO TABLE t_snwd_so.
IF sy-subrc EQ 0.
LOOP AT t_vbak ASSIGNING .
WRITE: / -vbeln,
-netwr,
-waerk.
ENDLOOP.
ENDIF.
SELECT ‘Sales Order:’ && vbeln AS vbeln,
netwr,
waerk
FROM vbak
UP TO 5 ROWS
INTO TABLE @DATA(t_vbak).
IF sy-subrc EQ 0.
LOOP AT t_vbak ASSIGNING FIELD-
SYMBOL().
WRITE: / -vbeln,
-netwr,
-waerk.
ENDLOOP.
ENDIF.

Arithmetic Calculations (passed to HANA database)

Arithmetic expressions are passed to HANA Database for the calculations.
Arithmetic Calculations are made, and functions called in a list of columns specified after SELECT.

  • Using the alternative column names defined after AS, the results are assigned to the columns with the same names of an internal table declared inline.

Arithmetic Expressions

TABLES: vbak.
SELECT-OPTIONS: s_sales FOR vbak-vbeln.
PARAMETERS : p_dis TYPE p DECIMALS 2.

SELECT vbeln, netwr,
netwr * @p_dis AS dis_amount,
waerk
FROM vbak
INTO TABLE @DATA(t_vbak)
WHERE vbeln IN @s_sales.

IF sy-subrc EQ 0.
LOOP AT t_vbak INTO DATA(wa_vbak).
WRITE: / wa_vbak-vbeln,
wa_vbak-netwr,
wa_vbak-dis_amount,
wa_vbak-waerk.
ENDLOOP.
ENDIF.

Use of ‘CEIL’ function (to round off the values) –

SELECT vbeln,
netwr,
CEIL( netwr * @p_dis ) AS dis_amount,
waerk
FROM vbak
INTO TABLE @DATA(t_vbak)
WHERE vbeln IN @s_sales.

IF sy-subrc EQ 0.
LOOP AT t_vbak INTO DATA(wa_vbak).
WRITE: / wa_vbak-vbeln,
wa_vbak-netwr,
wa_vbak-dis_amount,
wa_vbak-waerk.
ENDLOOP.
ENDIF.

Use of ‘ROUND’ function (to round off the values – specifies the number of decimal places)

SELECT vbeln,
netwr,
ROUND( netwr * @p_dis, 2 ) AS dis_amount,
waerk
FROM vbak
WHERE vbeln IN @s_sales
INTO TABLE @DATA(t_vbak).

IF sy-subrc EQ 0.
LOOP AT t_vbak INTO DATA(wa_vbak).
WRITE: / wa_vbak-vbeln,wa_vbak-netwr,
wa_vbak-dis_amount,
wa_vbak-waerk.
ENDLOOP.
ENDIF.

Case Expressions (WHEN… THEN… ELSE…)

A case expression allows the user to use – WHEN…THEN…ELSE logic, without using SQL Script in SQL
statements.

Simple Case Expression

CASE
WHEN THEN
[{ WHEN THEN }…]
[ ELSE ]
END

If the expression following the CASE statement is equal to the expression following the WHEN
statement, then the expression following the THEN statement is returned.
Otherwise, the expression following the ELSE statement is returned, if it exists.

Example

SELECT vbeln, netwr, waerk,
CASE auart
WHEN 'ZCRA' THEN 'Returns Authorize'
WHEN 'ZCRG' THEN 'Global Return Order'
WHEN 'ZCRS' THEN 'Specific Retn Order'
ELSE 'Outbound Order' END AS auart
FROM vbak
INTO TABLE @DATA(lt_vbak)
WHERE vbeln IN @s_sales.

IF sy-subrc EQ 0.
LOOP AT lt_vbak ASSIGNING FIELD-SYMBOL(<fs_vbak>).
IF <fs_vbak> IS ASSIGNED.
WRITE: / <fs_vbak>-vbeln,
<fs_vbak>-netwr,
<fs_vbak>-waerk,
<fs_vbak>-auart.
ENDIF.
ENDLOOP.
UNASSIGN <fs_vbak>.
ENDIF.

Search Case Expression

CASE
WHEN <condition> THEN <expression>
[{ WHEN <condition> THEN <expression>}…]
[ ELSE <expression>]
END

The CASE expression will always return a value (returning behaviour) and will always return a value
from the first matching part (return value behaviour).

Note: ‘CASE’ expressions cannot be used in WHERE conditions i.e., to fetch the data from the database
tables.

Example

SELECT vbeln, netwr, waerk,
CASE
WHEN netwr GT 10000
THEN 'High Order'
WHEN netwr GT 1000 AND netwr LE 10000 THEN 'Medium Order'
WHEN netwr LT 1000
THEN 'Low Order'
END AS order_type
FROM vbak
INTO TABLE @DATA(lt_vbak)
WHERE vbeln IN @s_sales.

IF sy-subrc EQ 0.
LOOP AT lt_vbak ASSIGNING FIELD-SYMBOL(<fs_vbak>).
IF <fs_vbak> IS ASSIGNED.
WRITE: / <fs_vbak>-vbeln,
<fs_vbak>-netwr,
<fs_vbak>-waerk,
<fs_vbak>-order_type.
ENDIF.
ENDLOOP.
UNASSIGN <fs_vbak>.
ENDIF.

Aggregate Expressions

An aggregate expression uses an aggregate function – to calculate a single value from the values of multiple rows in one or more columns.

Aggregate Functions –

Aggregate Functions are analytic functions that calculate an aggregate value based on a group of rows.

  • AVG function → returns the arithmetical mean of the expression.
  • COUNT function → counts the number of rows returned by a query.
  • MAX function → returns the maximum value of the expression.
  • SUM function → returns the sum of the expression.

Aggregate functions using ‘Group by’ clause & ‘Order by’ clause

‘GROUP BY’‘ORDER BY’
The ‘GROUP BY’ clause is used to group the rows with the same values.

In other words, ‘Group by’ clause controls the presentation of rows.
The ‘ORDER BY’ clause is used to group the columns with the same values.

In other words, ‘Order by’ clause controls the
presentation of the columns.
It is used together with the SQL SELECT statement.It sorts the result in ascending order by default like ASC and DESC
In SELECT statement, the GROUP BY clause is always used before the ORDER BY clause.In SELECT statement, the ORDER BY clause is always used after the GROUP BY clause.
Attributes cannot be used in the GROUP BY statement, under aggregate functions.Attributes can be used in the ORDER BY statement,
under aggregate functions.
  • In the SELECT statement, the GROUP BY clause can only be used – when the column(s) contains aggregate functions, constants and expressions.
  • In other words, the GROUP BY clause is often used with aggregate functions such as MAX(), MIN(), COUNT(), AVG(), etc.
  • In the ‘GROUP BY’ clause, it is mandatory to include all the fields / columns which are not part of the aggregate functions.

Example

SELECT
CASE auart
WHEN 'ZCRA' THEN 'Returns Authorize'
WHEN 'ZCRG' THEN 'Global Return Order'
WHEN 'ZCRS' THEN 'Specific Retn Order'
ELSE 'Outbound Order' END AS auart,
SUM( netwr ) AS netwr,
waerk
FROM vbak
INTO TABLE @DATA(lt_vbak)
WHERE vbeln IN @s_sales
GROUP BY auart, waerk.

IF sy-subrc EQ 0.
LOOP AT lt_vbak ASSIGNING FIELD-SYMBOL(<fs_vbak>).
IF <fs_vbak> IS ASSIGNED.
WRITE: / <fs_vbak>-auart,
<fs_vbak>-netwr,
<fs_vbak>-waerk.
ENDIF.
ENDLOOP.
UNASSIGN <fs_vbak>.
ENDIF.

Using ‘HAVING’ clause & ‘WHERE’ clause

‘HAVING’ clause‘WHERE’ clause
The HAVING clause is used to restrict the results
returned by the ‘GROUP BY’ clause.

In other words, the ‘HAVING’ clause is used to filter the records from the groups based on the given condition in the HAVING clause.
The WHERE clause is used to filter the records from
the tables.

Only those records can be extracted which are
satisfying the specified condition in WHERE clause.
The ‘HAVING’ clause cannot be used without GROUP BY clause.The WHERE clause can be used without GROUP BY
clause.
The ‘HAVING’ clause can only be used with the
SELECT statement. It implements in column
operations.
It can be used with SELECT, UPDATE & DELETE
statements. It implements in row operations.
The ‘HAVING’ clause is used after the GROUP BY
clause.
The ‘WHERE’ clause is used before the GROUP BY
clause.
The ‘HAVING’ clause can contain aggregate functions.The ‘WHERE’ clause cannot the aggregate functions.

Example

SELECT
CASE auart
WHEN 'ZCRA' THEN 'Returns Authorize'
WHEN 'ZCRG' THEN 'Global Return Order'
WHEN 'ZCRS' THEN 'Specific Retn Order'
ELSE 'Outbound Order' END AS auart,
SUM( netwr ) AS netwr,
waerk
FROM vbak
INTO TABLE @DATA(lt_vbak)
WHERE vbeln IN @s_sales
GROUP BY auart, waerk
HAVING SUM( netwr ) GT 0.

IF sy-subrc EQ 0.
LOOP AT lt_vbak ASSIGNING FIELD-SYMBOL(<fs_vbak>).
IF <fs_vbak> IS ASSIGNED.
WRITE: / <fs_vbak>-auart,
<fs_vbak>-netwr,
<fs_vbak>-waerk.
ENDIF.
ENDLOOP.
UNASSIGN <fs_vbak>.
ENDIF.

Aggregate functions using ‘COUNT’ (to count the number of rows)

COUNT the number of rows returned by a query. COUNT(*) returns the number of rows, regardless of the
value of those rows and including duplicate values.

Example

SELECT
CASE auart
WHEN 'ZCRA' THEN 'Returns Authorize'
WHEN 'ZCRG' THEN 'Global Return Order'
WHEN 'ZCRS' THEN 'Specific Retn Order'
ELSE 'Outbound Order' END AS auart,
COUNT( * ) AS sales_count
FROM vbak
INTO TABLE @DATA(lt_vbak)
WHERE vbeln IN @s_sales
GROUP BY auart
ORDER BY sales_count ASCENDING.

IF sy-subrc EQ 0.
LOOP AT lt_vbak ASSIGNING FIELD-SYMBOL(<fs_vbak>).
IF <fs_vbak> IS ASSIGNED.
WRITE: / <fs_vbak>-auart,
<fs_vbak>-sales_count.
ENDIF.
ENDLOOP.
UNASSIGN <fs_vbak>.
ENDIF.

Note: Sequence for SQL Server –

  • WHERE | GROUP BY | HAVING | ORDER BY

Program Flow Logic – Conditional Expressions

A conditional expression is a constructor expression that creates a value or raises a class-based exception
depending on a logical expression or a case distinction.

Conditional expressions are constructed using the following conditional operators:
➔ COND
➔ SWITCH

COND, Conditional Operator

Syntax –
COND #( WHEN … THEN …
WHEN … THEN …
….
ELSE …).

  • A conditional expression with the conditional operator SWITCH has a result, that is dependent on case distinction.
  • WHEN must be specified at least once with any logical expression in the parentheses. This can be followed by any number of WHEN statements with further expressions. An ELSE statement can be specified at the end.
  • The expression evaluates the logical expressions one after the other and selects the result specified after THEN of the first logical expression whose result is true. The selected result determines the result of the conditional expression.

Example

SWITCH string( sy-index
WHEN 1 THEN 'one'
WHEN 2 THEN 'two'
WHEN 3 THEN 'three'
ELSE THROW cx_overflow( ) ) ).

Important Points :-

  1. A conditional expression with SWITCH has the same meaning as the conditional expression with COND.
  2. In case of Conditional expression with SWITCH –
    • No Text symbols can be specified after WHEN.

VALUE, Value Operator

The Value Operator VALUE creates a result of a data type specified using TYPE.

Syntax:
… VALUE type( … ) …

  • When an initial value VALUE #( ) is passed to a generically typed formal parameter, the type is derived from the generic type.
  • The value operator VALUE can also be used to construct the content of existing complex data objects (structures and internal tables).
  • Arithmetic calculations with the results of VALUE for constructing values are not possible.

VALUE, Internal Tables

Construction of an internal table with a structured line type and filling it with two lines. The structures
are filled with values component by component.

TYPES: BEGIN OF t_struct,
col1 TYPE i,
col2 TYPE i,
END OF t_struct,
t_itab TYPE TABLE OF t_struct WITH EMPTY KEY.

DATA itab TYPE t_itab.

itab = VALUE #( ( col1 = 1 col2 = 2 )
( col1 = 3 col2 = 4 ) ).

Example

Construction of a ranges table and filling it with four lines while using the short form for structured line types.

DATA itab TYPE RANGE OF i.

itab = VALUE #( sign = 'I'
option = 'BT' ( low = 1 high = 10 )
( low = 21 high = 30 )
( low = 41 high = 50 )
option = 'GE' ( low = 61 ) ).

Construction of a range table for Materials based on internal table for materials.

DATA r_matnr TYPE RANGE OF matnr.

r_matnr = VALUE #( FOR ls_mara IN it_mara
sign
= 'I'
option = 'EQ'
( low
= ls_mara-matnr )
).

Table Expressions (for accessing table lines)

Prior to ABAP NW 7.4

READ TABLE flight_schedules INTO DATA(flight_schedule)
WITH KEY carrid = ‘AA’
connid = ‘0017’.

ABAP NW 7.4

The syntax for using a table expression consists of an internal table, followed by a row specified in square
brackets[ ].

IF line_exists( flight_schedules[ carrid = ‘AA’ connid = ‘0017’] ).
DATA(flight_schedule) = flight_schedules[ carrid = ‘AA’ connid = ‘0017’].
ENDIF.

The result of table expression is a single table line. If a table line is not found, the exception
CX_SY_ITAB_LINE_NOT_FOUND is raised.

First, we check the existence of the specified row, and if it exists then we can perform the table read.
IF line_exists( flight_schedules[ carrid = ‘AA’] ).
DATA(flight_schedule) = flight_schedules[ carrid = ‘AA’].
ENDIF.

In order to perform BINARY SEARCH – the internal table must be declared as a SORTED table.
The internal table definition

DATA: IT_MARA TYPE SORTED TABLE OF MARA WITH UNIQUE KEY MATNR.
IF line_exists( it_mara[ matnr = ‘00001008’] ).
DATA(wa_mara) = it_mara[ matnr = ‘00001008’].
ENDIF.

CORRESPONDING, Component Operator

The component operator CORRESPONDING constructs a result with the target type specified using ‘#’ from the components of a parameter struct or itab.

Example

  • Assigns the components of the structure struct1 to the components of the structure struct2 using matching names for the components.
  • An assignment of structures – struct2 = CORRESPONDING #( struct1 ).
  • Assigns the components of the structure struct1 to the components of the structure struct2 using mapping rules for the components.
DATA: BEGIN OF struct1,
mcomp1 TYPE i VALUE 1,
mcomp2 TYPE i VALUE 2,
END OF struct1.

DATA: BEGIN OF struct2,
comp1 TYPE i,
comp2 TYPE i,
END OF struct2.

An assignment of structures –
struct2 = CORRESPONDING #( struct1 MAPPING comp1 = mcomp1
comp2 = mcomp2).

NEW – Instance Operator

Instance Operator NEW creates an instance of a class.
The instance operator NEW always creates a new temporary reference variable that points to the new object.

Old Syntax [Before 7.40]

DATA: lo_demo_amdp_functions TYPE REF TO cl_demo_amdp_functions_inpcl.
CREATE OBJECT lo_demo_amdp_functions.

New Syntax [After 7.40]

DATA( lo_demo_amdp_functions ) = NEW cl_demo_amdp_functions_inpcl( ).
lo_demo_amdp_functions-> select_get_scarr_spfli(
EXPORTING clnt
= sy-mandt
carrid = carrid
IMPORTING scarr_spfli_tab = DATA(result1) ).

NEW cl_demo_amdp_functions_inpcl( )->select_get_scarr_spfli(
EXPORTING clnt
= sy-mandt
carrid = carrid
IMPORTING scarr_spfli_tab = DATA(result1) ).

Leave a Comment