Monday, December 30, 2019

Alternate for READ Table statement


ABAP HANA 7.51: Alternative to READ TABLE
There is now an alternate for ‘READ TABLE’ statement which is achieved with help of already existing ASSIGN statement. This is a good to have practice in newer ABAP coding convention if it is used wisely. Some important points to be noticed here:
  • An Internal table lookup can be done by using INDEX of the row
    • If an invalid index is used to reference data i.e. a row at referred index doesn’t exist, in that case, runtime exception CX_SY_ITAB_LINE_NOT_FOUND is thrown.
  • An Internal table lookup is possible by searching with a field-value pair.
  • The binary Search option is not available in this variant. All searches are essentially sequential search.
    • If Internal table data is huge and sorted, it is recommended to use READ TABLE… BINARY SEARCH option to achieve better performance.
Sample Source Code: Variant 1 – ASSIGN index
READ_TABLE_alt_Var1_source
Output: Variant 1 – ASSIGN index
READ_TABLE_alt_Var1_output
Sample Source Code: Variant 2 – READ with field
READ_TABLE_alt_Var2_source
The source code is given just for illustration. No output is provided for this example.
Sample Source Code: Variant 3
Runtime error “cx_sy_itab_line_not_found” is thrown if index can’t be read. This should be particularly taken care in advance to minimize chances of runtime errors.
READ_TABLE_alt_Var3_source
Output: Variant 3
READ_TABLE_alt_Var3_output

LINE_EXISTS

ABAP HANA 7.51: LINE_EXISTS Function
LINE_EXISTS is a table function which checks if the specified line exists in the Internal table or not. This returns either TRUE or FALSE. Argument of this function can be a simple check or a table expression. Some important points to be noted here:
  • LINE_EXISTS works as a READ TABLE with TRANSPORTING NO FIELDS which returns SY-SUBRC
  • This doesn’t return SY-TABIX or SY-INDEX which can indicate the line of match during search
Sample Source Code: Variant 1 – Search by Index
LINE_EXISTS_Var1_source
Output: Variant 1 – Search by Index
LINE_EXISTS_Var1_output
Sample Source Code: Variant 2 – Search by field-value pair
LINE_EXISTS_Var2_source
Output: Variant 2 – Search by field-value pair
LINE_EXISTS_Var2_output

No comments:

Post a Comment