Bu yazıyı okumak yaklaşık 3 dakika sürer.
When writing ABAP programs, we do not directly modify the data in the database. Instead, we use the SELECT command to fetch the desired data from the database and store it in a structure called an internal table. We then perform operations on this data as needed and finally update the database with the data from the internal table.
When fetching data from the database with SELECT, we do not always fetch data from a single table. Often, we fetch data from 2, 3, or even 4 or 5 tables simultaneously and transfer this data to an internal table. However, during this selection process, since the structure of each table is not the same, the data may not be correctly transferred to the internal table. By structure difference, I mean that the fields and the order of the fields of the selected tables are different.
For example, let’s see the transfer of data fetched from a table to an internal table using a standard SELECT statement:
report zae_corresponding.
data: gt_scarr type table of scarr,
gs_scarr type scarr.
start-of-selection.
select * from scarr
into table gt_scarr.
break-point.
The result of the above query is as follows:
Since we selected all fields, we can transfer them into the internal table using into table gt_scarr without any issues.
In the above example, we did not face any problems because we selected all fields and placed them into the gt_scarr content. But what if we wanted to fetch and transfer only 2 fields’ data into gt_scarr? Let’s examine the code below for an example:
report zae_corresponding.
data: gt_scarr type table of scarr,
gs_scarr type scarr.
start-of-selection.
select carrid carrname
from scarr
into table gt_scarr.
break-point.
The image of our query, which selected 2 fields (carrid, carrname), is as follows:
Since it tries to fit carrid into the mandt field and carrname into the carrid field, we encounter an issue like this.
To prevent the shifting of fields in the scarr table, we write code like below. The phrase “into corresponding fields of table” used here means the following according to the query below: Go to scarr, select the fields carrid and carrname, then if there are carrid and carrname fields in gt_scarr, transfer them to gt_scarr.
report zae_corresponding.
data: gt_scarr type table of scarr,
gs_scarr type scarr.
start-of-selection.
select carrid carrname
from scarr
into corresponding fields of table gt_scarr.
break-point.
After running our code written by specifying the phrase “into corresponding fields of table,” we get the image below, and it works correctly as expected.
Conclusion
You do not always select and transfer all the fields in the structure of the internal table you created with SELECT. Sometimes you select a few fields. In such a case, you can prevent column/data shifting that may occur by using the structure “into corresponding fields of table” instead of “into table.”
Have you encountered any difficulties in this regard?
You can read more about this topic in the official SAP resources from here.