0% found this document useful (0 votes)
18 views

Abap Read New Syntax 1730659135

Uploaded by

Srinivas Konga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Abap Read New Syntax 1730659135

Uploaded by

Srinivas Konga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

ABAP Tip: Modern Table Expressions – Simplifying READ Operations

Working with internal tables in ABAP has been greatly simplified with the introduction of
the new READ syntax. The traditional method using READ TABLE and sy-subrc has
evolved, providing a more concise and efficient way to handle data retrieval.

No More sy-subrc – Use Exceptions Instead

One of the key differences is that sy-subrc is no longer updated after a read operation.
Instead, if the read fails, an exception CX_SY_ITAB_LINE_NOT_FOUND is raised. This means
we need to handle these failures with TRY...CATCH blocks.

Here’s a quick example of how it looks:

TRY.
DATA(ls_flight) = it_flights[ carrid = 'AA' connid = '0026' ].
WRITE: 'Read successful'.
CATCH cx_sy_itab_line_not_found.
WRITE: 'Entry not found'.
ENDTRY.

No need to check sy-subrc anymore—just handle exceptions when the entry isn’t found. It's
clean, and it forces us to think about error handling more explicitly!

Table Expressions at Any Position

Table expressions can be used in any operand position, providing more flexibility. You can
use them directly in assignments, IF conditions, or even directly fetch fields from the result.
A specific field from a row can be retrieved directly in one line, making code more concise.

Here’s an example where we can directly read a field from a row:

DATA(plane_type) = it_flights[ carrid = 'AA' ]-planetype.

In just one line, get the planetype value from the matching entry. It’s concise and super
intuitive!

You can also use it in conditional blocks, making the code more readable:

IF it_flights[ connid = '0026' ]-planetype = 'A319'.


WRITE: 'This is an A319 plane.'.
ELSE.
WRITE: 'Another plane type.'.
ENDIF.

This saves us from writing multiple lines and manually reading the table and then comparing
fields.
Handling Optional Reads with OPTIONAL

Now, there are times when we don’t care if the read operation fails—we just want the code to
move on without throwing an exception. This is where the OPTIONAL keyword comes in
handy.

With OPTIONAL, if no match is found, the target variable is simply left empty, and you
avoid exceptions.

Example:

DATA(ls_flight) = VALUE #( it_flights[ carrid = 'AA' ] OPTIONAL ).

However, it’s important to note that OPTIONAL should be used when it is acceptable for the
structure to remain empty. It is not meant to bypass error handling where it is necessary.

Implicit Binary Search

We cannot use binary search explicitly in the new Read syntax. To overcome this, we can use
Sorted tables. Binary search is automatically applied in sorted tables. That means you don’t
need to explicitly mention BINARY SEARCH like we did before. Just define your internal table
as a SORTED TABLE, and ABAP will do the rest.

DATA: it_flights TYPE SORTED TABLE OF sflight WITH UNIQUE KEY carrid
connid.

SELECT * FROM sflight INTO TABLE it_flights.

DATA(ls_flight) = it_flights[ carrid = 'SQ' connid = '0026' ].

ABAP handles the binary search behind the scenes, ensuring optimal performance for large
datasets.

You might also like