TL; DR: In this ABAP beginner’s blog series, you’ll learn the fundamental concepts of internal tables (ITABs) and how to use them effectively in ABAP programming to store and manipulate data.
An internal table in ABAP is a dynamic data structure used temporarily during the runtime of an SAP program to process possibly large volumes of data. It stores multiple data records in rows and columns with a consistent structure and key. Internal tables (ITABs) are accessed using keys and are discarded when the program execution ends. They serve as temporary storage areas, allowing data modification as needed. The size of ITABs is flexible and handled by the ABAP runtime system. These tables can have multiple rows. ITABs, depending on the table type, are consisting of various fields, including non-unique keys, allowing duplicate records. They are employed for calculations, quick data access, and versatile applications.
In this section, we explain some basic uses of ITABs with an example and their results. Let’s assume, a friend is coming on a short visit to Germany, seeking to explore both vibrant places and scenic hiking trails. To meet the perfect itinerary, we have compiled the following list of ten popular places to visit in Germany that offer nearby hiking opportunities:
Based on this table, we are going to explore the basic uses of ITABs by answering the following questions:
- How can we add the values to the internal table?
- How can we append a new row to the ITAB?
- How many lines does the ITAB contain?
- How can we sort the ITAB?
- How can we read a row from the ITAB that contains a specific value?
- How can we delete one row from the ITAB?
- How can we delete duplicate rows?
At the beginning, we defined a structure ‘place_type’ which includes three fields: state, place, and trail with respective data type. Next, the ‘itab_popularplaces’ is declared as the data type for the ITAB, which allows multiple lines and maintains the entries in the order they were inserted. Finally, the ‘popularplaces’ is the variable that represents the actual internal table, instantiated using the ‘itab_popularplaces’ data type. It will hold the data records, where each entry will follow the structure defined by ‘place_type’.
1. How can we add the values to the internal table?
Solution: We populate the ITAB component by component with VALUE operator.
Here is the sample program to populate the ITAB.
We can print the ITAB in the ABAP console using the commands:
2. How can we append a new row to the ITAB?
Suppose we have a new suggestion about a place, and we want to append the row to the ITAB.
Solution: We can append a new row at the end of the ITAB with APPEND operator.
3. How many lines does the ITAB contain?
We can count the lines contain the ITAB by using following sample program:
4. How can we sort the ITAB?
In ABAP, we can sort the ITABs by using SORT operator. This sorting can be done based on column and ascending-descending order. In this example, we have sorted by column ‘state’ in ascending order.
5. How can we read a row from the ITAB that contains a specific value?
There are multiple ways to read any specific row from the ITAB, providing flexibility in how the data is retrieved and manipulated. In this example, we have used a loop…endloop statement to read the specific row in the ITAB that contains the place ‘Berlin’.
6. How can we delete one row from the ITAB?
We can delete any row from ITAB by using DELETE operator, including specifying the row number. For example, we can delete index 11 with the following command line.
7. How can we delete duplicate rows?
In ABAP, the DELETE ADJACENT DUPLICATES statements give us the ability to remove all consecutive lines, excluding the first occurrence. Prior to utilizing these statements, it is essential to perform suitable sorting of the data. For this example, we have chosen state column and the duplicate states (e.g., Bavaria) are deleted.
In conclusion, ITABs are a crucial part of ABAP and particularly important to store and format data from a database table within a program. Are you ready to dive into ITABs – the essential building block of ABAP development? Keep your eyes on our beginner blog series!
For more information, see the SAP documentation: