This is a test message to test the length of the message box.
Login
New ABAP - Read internal tables
Created by Software-Heroes

ABAP - Internal tables (read)

2363

New functions and commands have been implemented for accessing internal tables. How these work and what to consider, we show you here.



Previously, internal tables were always read and evaluated via READ TABLE, which has changed somewhat with the new way of ABAP. Of course, what you knew so far is not outdated, but there are a few shorter ways to get the data in the tables.

In today's article, we want to focus on the different access methods, what you should look for, and how exactly the accesses work.

 

Read over the index

A common method is to read the first or last line of an internal table. To do this, use the index of the table to read the correct line. Whether if the entry was found, you have to check with the subrc.

The new version reads the index like reading an array in other languages. You access the index directly. But beware, if the line does not exist, because then an exception is raised and if this is not catched, then the report creates a short dump.


DATA:
  lt_t001 TYPE STANDARD TABLE OF t001,
  ls_t001 TYPE t001.

" old function
READ TABLE lt_t001 INTO ls_t001 INDEX 1.

" new function
ls_t001 = lt_t001[ 1 ].

" with inline declaration
DATA(ls_t001) = lt_t001[ 1 ].

 

Read over keys and fields

When reading the table, in most cases it is read with a predefined key or via fields defined as a free key. This syntax is still possible and slightly more compact in the new form and also easy to read for a few keys. As in the case of reading via the index, a direct inline declaration can also be used in this case.


" alter Read
READ TABLE lt_t001 INTO ls_t001
 WITH KEY bukrs = 'ABCD'.

" neuer Read
ls_t001 = lt_t001[ bukrs = 'ABCD' ].

 

Examination of the result

Before you read lines in the new variant, you should make sure that you catch errors or early enough checks whether the desired line is even present. The new standard offers two options that you can use.

In the first case you can use the new command LINE_EXISTS to check if the desired line exists.


" Examine the line
IF LINE_EXISTS( lt_t001[ 1 ] ).
  DATA(ls_t001) = lt_t001[ 1 ].
ENDIF.

 

In the second case, you catch the error while reading and save yourself the double typing of the key conditions. These methods provide the best solution in most cases.


" Examine the line
TRY.
    DATA(ls_t001) = lt_t001[ 1 ].
  CATCH CX_SY_ITAB_LINE_NOT_FOUND.
    " Error handling
ENDTRY.

 

Read only a field

In addition to the definition of an entire structure, it is also possible to access individual fields during reading, if, for example, in our case only the text for the company code is to be read.


" Read the text
DATA(ld_text) = lt_t001[ bukrs = 'ABCD' ]-butxt.

" Read the currency
DATA(ld_waers) = lt_t001[ bukrs = 'ABCD' ]-waers.

 

Conclusion

In today's article, we've shown you how the new accesses to internal tables work and how easy they are now. But there are also some new things that need to be considered when accessing and that the subrc is not quite as important as it used to be.

 

Source:
SAP Documentation table expressions


Included topics:
New ABAPInternal tablesLINE_EXISTS
Comments (0)



And further ...

Are you satisfied with the content of the article? We post new content in the ABAP area every Friday and irregularly in all other areas. Take a look at our tools and apps, we provide them free of charge.


ABAP - Type Casting

Category - ABAP

How do you actually get the original type of a class or instance if it is passed in a generic table? In this article we examine the possibilities.

04/16/2024

ABAP - RETURN value

Category - ABAP

After all these years, the “real” return in ABAP has finally arrived. In this article we will show you how it works and what it can do.

02/13/2024

ABAP Deep Dive - FOR (Loops)

Category - ABAP

Let's take a closer look at the FOR loop. How does it work? What do I have to consider and what can I do with it?

04/14/2023

ABAP Deep Dive - Table access (internal)

Category - ABAP

In this article, let's take a look at table access to internal tables and how they replace READ TABLE.

02/03/2023

ABAP Developer still relevant

Category - ABAP

In this article we look at whether ChatGPT can already replace an ABAP developer or whether it can be used as a help in everyday life.

01/06/2023