Wednesday 17 May 2023

SAP ABAP 7.4: For Loop with Multiple table – Append all the data to final Internal table

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.

SAP ABAP 7.4, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Prep, SAP ABAP Preparation, SAP ABAP Tutorial and Materials
Multiple For Loop with Multiple Tables

Result:

SAP ABAP 7.4, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Prep, SAP ABAP Preparation, SAP ABAP Tutorial and Materials
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.

SAP ABAP 7.4, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Prep, SAP ABAP Preparation, SAP ABAP Tutorial and Materials
For loop with Let Statement for Multiple Tables

Result

SAP ABAP 7.4, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Prep, SAP ABAP Preparation, SAP ABAP Tutorial and Materials
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.

No comments:

Post a Comment