ABAP - Internal tables (read)
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.
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.