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)

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

Advertising

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)
Advertising

ABAP - Performance for the SELECT

Category - ABAP

In this article we will look at a few special cases with the SELECT and examine the performance of these constructs. We'll show you the current alternatives and give you little tips while reading.

04/02/2021

ABAP - Check objects (Instances)

Category - ABAP

In this article we will show you how you can analyze instances and react to them correctly, for example if you hand them over during processing and want to react individually.

03/19/2021

ABAP - Loops

Category - ABAP

With New ABAP, new loops and possibilities for restricting table contents were created. We'll take a closer look at these new commands in this article.

02/19/2021

ABAP - Comparison

Category - ABAP

Today we'll look at comparisons and comparison operators in terms of the new commands and their current usage. What has changed so far and what should still be done?

02/12/2021

ABAP - String Functions (Part 3)

Category - ABAP

This article is about other new string functions and a replacement for CONDENSE. We would like to show you why you should use the new functions.

01/22/2021

We use cookies for our services and functions. More informations