Tuesday, August 1, 2017

Return value of READ TABLE statements

Returning value or sometimes technically called sy-subrc is very important while programming in ABAP language. It is also important when it comes to READ TABLE statement of the ABAP.

Normally one would expect that the READ TABLE returns two values. Zero (0) in case the READ was successfully (at least one line was read) and four (4) in case it wasn’t. However there are more of the return values.

There is also value of eight (8). It is similar to 4. The 8 is returned in the case search was done with binary method and/or key was not fully qualified.

There is also value of two (2). The 2 is returned in the case the READ statement has an addition COMPARING. To demonstrate this in more detail I’m providing below example of ABAP program based on fragment code I found in online documentation.

REPORT ZMM_READ_TABLE.

DATA: BEGIN OF ls_line,
  col1 TYPE i,
  col2 TYPE i,
END OF ls_line.
DATA lt_tab LIKE HASHED TABLE OF ls_line WITH UNIQUE KEY col1.

WRITE: / 'DATA in TABLE:'.
WRITE: / 'lt_tab-col1'20 'lt_tab-col2'.
DO 4 TIMES.
  ls_line-col1 = sy-index.
  ls_line-col2 = sy-index ** 2.
INSERT ls_line INTO TABLE lt_tab.
WRITE: / ls_line-col1, 20 ls_line-col2.
ENDDO.
SKIP.

WRITE: / 'DATA to be FOUND in structure:'.
ls_line-col1 = 2. ls_line-col2 = 3.
WRITE: / 'ls_line-col1'20 'ls_line-col2'.
WRITE: / ls_line-col1, 20 ls_line-col2.

SKIP.
WRITE: / 'READ TABLE lt_tab FROM ls_line INTO ls_line. ==>'.
READ TABLE lt_tab FROM ls_line INTO ls_line.
WRITE: 'SY-SUBRC =', sy-subrc.
WRITE: / 'row found:', sy-tabix, ls_line-col1, ls_line-col2.

SKIP.
ls_line-col1 = 2. ls_line-col2 = 3.
WRITE: / 'READ TABLE lt_tab FROM ls_line INTO ls_line COMPARING col2. ==>'.
READ TABLE lt_tab FROM ls_line INTO ls_line COMPARING col2.
WRITE: 'SY-SUBRC =', sy-subrc.
WRITE: / 'row found:', sy-tabix, ls_line-col1, ls_line-col2.

SKIP.
ls_line-col1 = 2. ls_line-col2 = 3.
WRITE: / 'READ TABLE lt_tab FROM ls_line INTO ls_line COMPARING col1. ==>'.
READ TABLE lt_tab FROM ls_line INTO ls_line COMPARING col1.
WRITE: 'SY-SUBRC =', sy-subrc.

WRITE: / 'row found:', sy-tabix, ls_line-col1, ls_line-col2.


No comments: