Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
ronak_jain2
Explorer
Hello,

Many of you already know to work with For loop in SAP ABAP. However, those who are new to the ABAP 7.4 syntax often struggle at some places to achieve the expected result using new syntax. One of the scenario, I am covering below so that new learner can find help with this post.

Scenario:

When we process multiple internal table inside the for loop(in which the where condition of the second internal table will be from the first internal table inside the For Loop). In this case, the result in the final internal table will be only the common data in which they find in both the internal tables.

As you can see below, GT_N has two rows. While GT_C has only one row. When we are processing these two internal tables, only the common data between these two tables is processed completely. If the data with the similar key is not present in both the internal tables then it does not process and skips the complete execution for that particular row and moves to the next row in the first table. Finally the result will be only the common data between the two tables.


Multiple For Loop with Multiple Tables


 
REPORT zrj_test.


TYPES: BEGIN OF ty_n,
numbers TYPE i,
END OF ty_n.

TYPES: BEGIN OF ty_c,
numbers TYPE i,
text TYPE char20,
END OF ty_c.



TYPES: ty_t_n TYPE STANDARD TABLE OF ty_n WITH EMPTY KEY,
ty_t_c TYPE STANDARD TABLE OF ty_c WITH DEFAULT KEY.

DATA(gt_n) = VALUE ty_t_n( ( numbers = 1 ) ( numbers = 2 ) ).
DATA(gt_c) = VALUE ty_t_c( ( numbers = 2 text = 'Two' ) ).
DATA(gt_o) = VALUE ty_t_c( FOR gs_n IN gt_n
FOR gs_c IN gt_c WHERE ( numbers = gs_n-numbers )
( numbers = gs_n-numbers
text = gs_c-text ) ).

cl_demo_output=>display( gt_o ).

Result:


Result for Multiple For Loop with Multiple Tables


Sometimes, we have a case, even if the record is not found in the second table then too we need to append the data of first table to the final internal table with the blank values for which the record is not found in the second tables. To achieve this, we can use LET keyword inside the for loop.


For loop with Let Statement for Multiple Tables



REPORT zrj_test.


TYPES: BEGIN OF ty_n,
numbers TYPE i,
END OF ty_n.

TYPES: BEGIN OF ty_c,
numbers TYPE i,
text TYPE char20,
END OF ty_c.



TYPES: ty_t_n TYPE STANDARD TABLE OF ty_n WITH EMPTY KEY,
ty_t_c TYPE STANDARD TABLE OF ty_c WITH DEFAULT KEY.

DATA(gt_n) = VALUE ty_t_n( ( numbers = 1 ) ( numbers = 2 ) ).
DATA(gt_c) = VALUE ty_t_c( ( numbers = 2 text = 'Two' ) ).
DATA(gt_o) = VALUE ty_t_c( FOR gs_n IN gt_n
LET lv_text = VALUE char20( gt_c[ numbers = gs_n-numbers ]-text OPTIONAL )
IN text = lv_text
( numbers = gs_n-numbers ) ).

cl_demo_output=>display( gt_o ).

Result


Result - For loop with Let Statement for Multiple tables


 

Using the LET keyword, For loop does not skip the record and process every record completely. Also, every record is appended in the final internal table. For the one which does not have the records in the second internal table is appended in the final table with the blank value for that particular column.

Thanks for taking your time to read this blog. I hope this will help you to achieve these kind of scenario easily. Also, you can check SAP Help site to see more examples with For Loop with different expressions.

Kindly share your comments, if you find this useful. Also, share your ideas in comments if you have used different expression with new ABAP 7.4 syntax to meet your scenario.
9 Comments
Labels in this area