Direkt zum Inhalt wechseln

An Introduction to ABAP Based on Basics of Internal Tables (ITABs) Example

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.

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:

StatePlaceTrail
BrandenburgBerlinBriesetal
BavariaSchloss NeuschwansteinAllgaeu
Baden-WuerttembergBlack ForestBlack Forest
HessenRuedesheimRheinsteig
BavariaMunichPartnach Gorge
SaxonyDresdenPainter’s Way
BavariaNurembergGoldsteig
Baden-WuerttembergHeidelbergAlbsteig
North Rhine-WestphaliaCologneEifelsteig
Rhineland-PalatinateTrierMoselsteig

Based on this table, we are going to explore the basic uses of ITABs by answering the following questions:

  1. How can we add the values to the internal table?
  2. How can we append a new row to the ITAB?
  3. How many lines does the ITAB contain?
  4. How can we sort the ITAB?
  5. How can we read a row from the ITAB that contains a specific value?
  6. How can we delete one row from the ITAB?
  7. How can we delete duplicate rows?

Step 0: 

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:  

The output:

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.

StatePlaceTrail
Saxony-AnhaltMagdeburgGoethe way


Solution: We can append a new row at the end of the ITAB with APPEND operator.  

The output:

3. How many lines does the ITAB contain?

We can count the lines contain the ITAB by using following sample program:

The output:

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.

The output:

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’.

The output:

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.  

The output:

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.

The output:

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!

Additional information.

For more information, see the SAP documentation:
English: https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abenitab_guidl.htm 
German: https://help.sap.com/doc/abapdocu_750_index_htm/7.50/de-de/abenitab_guidl.htm

Liebe Besucher,

wir haben Neuigkeiten: Seit dem 22.10.2024 sind wir eine AG und haben ein neues Logo. 🎉
Wir freuen uns, noch flexibler und transparenter in die Zukunft zu starten.

Das Team der j&s-soft AG