Abap Read New Syntax 1730659135
Abap Read New Syntax 1730659135
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.
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.
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 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.
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:
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:
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.
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.
ABAP handles the binary search behind the scenes, ensuring optimal performance for large
datasets.