Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Runtime error while accessing Internal table with Key (ABAP 7.4)

VijayaKrishnaG
Active Contributor
16,781

Dear Experts,

With some reference, I have written below statement instead of normal Read statement to get an entry from an Internal table with some Key. But as the entry not existing with the passed key, process goes to runtime error.

ABAP Statement:

 DATA(ls_ekko) = gt_ekko[ ebeln = ls_bseg-ebeln ].
 IF sy-subrc = 0.
   ls_output = VALUE zncms0002( bedat = ls_ekko-bedat ).
 ENDIF.

"In the above statement LS_BSEG-EBELN is blank. 

Runtime Error:

The reason for the exception is: Access failed for table "GT_EKKO". Access method: "KEY". Line index (for "INDEX" access) / key name (for "KEY" access): "<free key>". Key values: " " " " " " " "

Can anyone please let me know how can we overcome this runtime error and use as general READ statement?

1 ACCEPTED SOLUTION

joltdx
Active Contributor
8,850

14434537ae0441799e327b92f10dfe46 suggested a good blog in the comment.

There is also information to read in the ABAP documentation.

The cx_sy_itab_line_not_found is what is thrown when no line is found, instead of sy-subrc for READ TABLE, so you need to catch that one as suggested.

There is also the possibility to using the value operator here, depending on your exact use case/need:

ekko_bedat = VALUE #( gt_ekko[ ebeln = ls_bseg-ebeln ]-bedat OPTIONAL ).

Using the OPTIONAL parameter will NOT cause the exception to be thrown if no line is found. Instead the 'ekko_bedat' will be INITIAL (whatever that is for the current data type) so you would still have to check it before you use it further down in the code...

5 REPLIES 5

luis_sismeiro
Participant
8,850

"If a table line is not found, the exception CX_SY_ITAB_LINE_NOT_FOUND is raised. No sy-subrc from expressions, of course."

Please refer to: https://blogs.sap.com/2013/05/29/abap-news-for-release-740-table-expressions/

I Think that you could try someting like this:

TRY. 
    DATA(ls_ekko) = gt_ekko[ ebeln = ls_bseg-ebeln ].
   CATCH cx_sy_itab_line_not_found.
ENDTRY.

VijayaKrishnaG
Active Contributor
0 Kudos
8,850

Hi Luis,

Thanks for your quick response / solution and blog. Now I would like to ask, can these statement meant for replacement of Read statement? And is there any performance differences in Read and the above statement, as we are not using any search techniques (Binary)?

joltdx
Active Contributor
8,851

14434537ae0441799e327b92f10dfe46 suggested a good blog in the comment.

There is also information to read in the ABAP documentation.

The cx_sy_itab_line_not_found is what is thrown when no line is found, instead of sy-subrc for READ TABLE, so you need to catch that one as suggested.

There is also the possibility to using the value operator here, depending on your exact use case/need:

ekko_bedat = VALUE #( gt_ekko[ ebeln = ls_bseg-ebeln ]-bedat OPTIONAL ).

Using the OPTIONAL parameter will NOT cause the exception to be thrown if no line is found. Instead the 'ekko_bedat' will be INITIAL (whatever that is for the current data type) so you would still have to check it before you use it further down in the code...

Sandra_Rossi
Active Contributor
0 Kudos
8,850

or

ls_output = VALUE #( bedat = VALUE #( gt_ekko[ ebeln = ls_bseg-ebeln ]-bedat OPTIONAL ) ).

VijayaKrishnaG
Active Contributor
0 Kudos
8,850

Hello All,

I would like to say 14434537ae0441799e327b92f10dfe46's comment is also very much helpful to resolve my issue. Thank you!