ABAP 7.40 Quick Reference - SAP Blogs
ABAP 7.40 Quick Reference - SAP Blogs
Contents
1. Inline Declarations
2. Table Expressions
3. Conversion Operator CONV
4. Value Operator VALUE
5. FOR operator
6. Reduction operator REDUCE
7. Conditional operators COND and SWITCH
8. CORRESPONDING operator
9. Strings
10. Loop at Group By
11. Classes/Methods
12. Meshes
13. Filter
14. Document Purpose
1. Inline Declarations
Before 7.40 With 7.40
Data
DATA text TYPE string. DATA(text) = 'ABC'.
text = 'ABC'.
Loop at
into work DATA wa like LINE OF itab. LOOP AT itab INTO DATA(wa).
area LOOP AT itab INTO wa. ...
... ENDLOOP.
ENDLOOP.
Call
method DATA a1 TYPE ... oref->meth(
DATA a2 TYPE ... IMPORTING p1 = DATA(a1)
IMPORTING p2 = DATA(a2) ).
oref->meth(
IMPORTING p1 = a1
IMPORTING p2 = a2 ).
https://blogs.sap.com/2015/10/25/abap-740-quick-reference/ 1/17
12/14/23, 11:30 PM ABAP 7.40 Quick Reference | SAP Blogs
Loop at
assignin FIELD-SYMBOLS: <line> type … LOOP AT itab
g ASSIGNING FIELD-SYMBOL(<line>).
LOOP AT itab ASSIGNING <line>. ...
... ENDLOOP.
ENDLOOP.
Read
assignin FIELD-SYMBOLS: <line> type … READ TABLE itab
g ASSIGNING FIELD-SYMBOL(<line>).
READ TABLE itab
ASSIGNING <line>.
Select
into table
DATA itab TYPE TABLE OF dbtab. SELECT * FROM dbtab
INTO TABLE @DATA(itab)
SELECT * FROM dbtab WHERE fld1 = @lv_fld1.
INTO TABLE itab
WHERE fld1 = lv_fld1.
Select
single SELECT SINGLE f1 f2 SELECT SINGLE f1 AS my_f1,
into FROM dbtab f2 AS abc
INTO (lv_f1, lv_f2) FROM dbtab
WHERE ... INTO DATA(ls_struct)
WHERE ...
WRITE: / lv_f1, lv_f2.
WRITE: / ls_struct-my_f1,
ls_struct-abc.
2. Table Expressions
If a table line is not found, the exception CX_SY_ITAB_LINE_NOT_FOUND is raised. No sy-subrc.
Read
Table READ TABLE itab INDEX idx wa = itab[ KEY key INDEX idx ].
using USING KEY key
key INTO wa.
https://blogs.sap.com/2015/10/25/abap-740-quick-reference/ 2/17
12/14/23, 11:30 PM ABAP 7.40 Quick Reference | SAP Blogs
Read
Table READ TABLE itab wa = itab[ col1 = … col2 = … ].
with WITH KEY col1 = …
key col2 = …
INTO wa.
Read
Table READ TABLE itab wa = itab[ KEY key col1 = …
with WITH TABLE KEY key col2 = … ].
key COMPONENTS col1 = …
comp col2 = …
onent INTO wa.
s
Does
record READ TABLE itab ... IF line_exists( itab[ ... ] ).
exist? TRANSPORTING NO FIELDS. ...
ENDIF.
IF sy-subrc = 0.
...
ENDIF.
Get
table
index DATA idx type sy-tabix. DATA(idx) =
line_index( itab[ ... ] ).
READ TABLE ...
TRANSPORTING NO FIELDS.
idx = sy-tabix.
NB: There will be a short dump if you use an inline expression that references a non-existent record.
SAP says you should therefore assign a field symbol and check sy-subrc.
II. Example
Method cl_abap_codepage=>convert_to expects a string
Before 7.40
helper = text.
OR
https://blogs.sap.com/2015/10/25/abap-740-quick-reference/ 4/17
12/14/23, 11:30 PM ABAP 7.40 Quick Reference | SAP Blogs
itab = VALUE #( ( ) ( 1 ) ( 2 ) ).
5. FOR operator
I. Definition
FOR wa|<fs> IN itab [INDEX INTO idx] [cond]
II. Explanation
This effectively causes a loop at itab. For each loop the row read is assigned to a work area (wa) or field-symbol(<fs>).
This wa or <fs> is local to the expression i.e. if declared in a subrourine the variable wa or <fs> is a local variable of
Given:
https://blogs.sap.com/2015/10/25/abap-740-quick-reference/ 5/17
12/14/23, 11:30 PM ABAP 7.40 Quick Reference | SAP Blogs
TYPES: ty_ships TYPE SORTED TABLE OF ty_ship WITH UNIQUE KEY tknum.
TYPES: ty_citys TYPE STANDARD TABLE OF ort01 WITH EMPTY KEY.
III. Example 1
Populate internal table GT_CITYS with the cities from GT_SHIPS.
Before 7.40
IV. Example 2
Populate internal table GT_CITYS with the cities from GT_SHIPS where the route is
R0001.
Before 7.40
Note: ls_ship does not appear to have been declared but it is declared implicitly.
https://blogs.sap.com/2015/10/25/abap-740-quick-reference/ 6/17
12/14/23, 11:30 PM ABAP 7.40 Quick Reference | SAP Blogs
TYPES:
BEGIN OF ty_line,
col1 TYPE i,
col2 TYPE i,
col3 TYPE i,
END OF ty_line,
ty_tab TYPE STANDARD TABLE OF ty_line WITH EMPTY KEY.
Before 7.40
DO.
j = j + 10.
IF j > 40. EXIT. ENDIF.
APPEND INITIAL LINE TO gt_itab ASSIGNING <ls_tab>.
<ls_tab>-col1 = j.
<ls_tab>-col2 = j + 1.
<ls_tab>-col3 = j + 2.
ENDDO
https://blogs.sap.com/2015/10/25/abap-740-quick-reference/ 7/17
12/14/23, 11:30 PM ABAP 7.40 Quick Reference | SAP Blogs
III. Example 1
Count lines of table that meet a condition (field F1 contains “XYZ”).
Before 7.40
IV. Example 2
Sum the values 1 to 10 stored in the column of a table defined as follows
Before 7.40
V. Example 3
Using a class reference – works because “write” method returns reference to instance object
With 7.40
I. Definition
… COND dtype|#( WHEN log_exp1 THEN result1
[ WHEN log_exp2 THEN result2 ]
…
[ ELSE resultn ] ) …
… SWITCH dtype|#( operand
WHEN const1 THEN result1
[ WHEN const2 THEN result2 ]
…
[ ELSE resultn ] ) …
DATA(time) =
COND string(
WHEN sy-timlo < '120000' THEN
|{ sy-timlo TIME = ISO } AM|
WHEN sy-timlo > '120000' THEN
|{ CONV t( sy-timlo - 12 * 3600 )
TIME = ISO } PM|
WHEN sy-timlo = '120000' THEN
|High Noon|
ELSE
THROW cx_cant_be( ) ).
DATA(text) =
NEW class( )->meth(
SWITCH #( sy-langu
WHEN 'D' THEN `DE`
WHEN 'E' THEN `EN`
ELSE THROW cx_langu_not_supported( ) ) ).
8. Corresponding Operator
I. Definition
… CORRESPONDING type( [BASE ( base )] struct|itab [mapping|except] )
With 7.40
DATA(ls_line2) = VALUE line2( col1 = 4 col2 = 5 col3 = 6 ).
WRITE: / 'ls_line2 =' ,15 ls_line2-col1, ls_line2-col2, ls_line2-col3.
SKIP 2.
III. Output
IV. Explanation
Given structures ls_line1 & ls_line2 defined and populated as above.
2
MOVE-CORRESPONDING ls_line1 ls_line2 = CORRESPONDING #
TO ls_line2. ( BASE ( ls_line2 ) ls_line1 ).
3
DATA: ls_line3 like ls_line2. DATA(ls_line3) = CORRESPONDING line2
( BASE ( ls_line2 ) ls_line1 ).
ls_line3 = ls_line2.
MOVE-CORRESPONDING ls_line1
TO ls_line2.
1. The contents of ls_line1 are moved to ls_line2 where there is a matching column name. Where there is no
match the column of ls_line2 is initialised.
2. This uses the existing contents of ls_line2 as a base and overwrites the matching columns from ls_line1.
3. This creates a third and new structure (ls_line3) which is based on ls_line2 but overwritten by matching
https://blogs.sap.com/2015/10/25/abap-740-quick-reference/ 10/17
12/14/23, 11:30 PM ABAP 7.40 Quick Reference | SAP Blogs
columns of ls_line1.
… MAPPING t1 = s1 t2 = s2
EXCEPT allows you to list fields that must be excluded from the data transfer
… EXCEPT {t1 t2 …}
9. Strings
I. String Templates
A string template is enclosed by two characters “|” and creates a character string.
Literal text consists of all characters that are not in braces {}. The braces can contain:
data objects,
calculation expressions,
constructor expressions,
table expressions,
predefined functions, or
Before 7.40
cl_demo_output=>display( output ).
II. Concatenation
https://blogs.sap.com/2015/10/25/abap-740-quick-reference/ 11/17
12/14/23, 11:30 PM ABAP 7.40 Quick Reference | SAP Blogs
Before 7.40
III. Width/Alignment/Padding
IV. Case
V. ALPHA conversion
DATA(lv_vbeln) = '0000012345'.
WRITE / |{ lv_vbeln ALPHA = OUT }|. “or ALPHA = IN to go in other direction
I. Definition
LOOP AT itab result [cond] GROUP BY key ( key1 = dobj1 key2 = dobj2 …
[gs = GROUP SIZE] [gi = GROUP INDEX] )
[ASCENDING|DESCENDING [AS TEXT]]
[WITHOUT MEMBERS]
[{INTO group}|{ASSIGNING <group>}]
…
[LOOP AT GROUP group|<group>
…
ENDLOOP.]
…
ENDLOOP.
https://blogs.sap.com/2015/10/25/abap-740-quick-reference/ 12/17
12/14/23, 11:30 PM ABAP 7.40 Quick Reference | SAP Blogs
II. Explanation
The outer loop will do one iteration per key. So if 3 records match the key there will only be one iteration for these 3
records. The structure “group” (or
“<group>” ) is unusual in that it can be looped over using the “LOOP AT GROUP” statement. This will loop over the 3
records (members) of the group. The
structure “group” also contains the current key as well as the size of the group and index of the group ( if GROUP SIZE
and GROUP INDEX have been
assigned a field name). This is best understood by an example.
III. Example
With 7.40
CLEAR: gv_tot_age.
https://blogs.sap.com/2015/10/25/abap-740-quick-reference/ 13/17
12/14/23, 11:30 PM ABAP 7.40 Quick Reference | SAP Blogs
With 7.40
LOOP AT GROUP <group> ASSIGNING FIELD-SYMBOL(<ls_member>).
gv_tot_age = gv_tot_age + <ls_member>-age.
WRITE: /13 <ls_member>-name.
ENDLOOP.
"Average age
gv_avg_age = gv_tot_age / <group>-size.
WRITE: / |Average age: { gv_avg_age }|.
SKIP.
ENDLOOP.
IV. Output
11. Classes/Methods
I. Referencing fields within returned structures
Before 7.40
ls_lfa1= My_Class=>get_lfa1( ).
lv name1 = ls lfa1-name1
https://blogs.sap.com/2015/10/25/abap-740-quick-reference/ 14/17
12/14/23, 11:30 PM ABAP 7.40 Quick Reference | SAP Blogs
Before 7.40
IF My_Class=>return_boolean( ) = abap_true.
…
ENDIF.
NB: The type “BOOLEAN” is not a true Boolean but a char1 with allowed values X,- and <blank>.
Using type “FLAG” or “WDY_BOOLEAN” works just as well.
Before 7.40
12. Meshes
Allows an association to be set up between related data groups.
I. Problem
https://blogs.sap.com/2015/10/25/abap-740-quick-reference/ 15/17
12/14/23, 11:30 PM ABAP 7.40 Quick Reference | SAP Blogs
Populated as follows:
1 Jason 3000
2 Thomas 3200
II. SolutionGet the details of Jerry’s manager and all developers managed by Thomas.
With 7.40
III. Output
Jerry’s manager: Jason Salary: 3000
https://blogs.sap.com/2015/10/25/abap-740-quick-reference/ 16/17
12/14/23, 11:30 PM ABAP 7.40 Quick Reference | SAP Blogs
Thomas’ developers:
13. Filter
Filter the records in a table based on records in another table.
I. Definition
… FILTER type( itab [EXCEPT] [IN ftab] [USING KEY keyname]
WHERE c1 op f1 [AND c2 op f2 […]] )
II. Problem
Filter an internal table of Flight Schedules (SPFLI) to only those flights based on a filter table that contains the fields
Cityfrom and CityTo.
III. Solution
With 7.40
Note: using the keyword “EXCEPT” (see definition above) would have returned the exact opposite records i.e all records
EXCEPT for those those returned above.
https://blogs.sap.com/2015/10/25/abap-740-quick-reference/ 17/17