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: 

SY-SUBRC EQ '8' for a SORTED Internal Table even though the record is READ - Table Expressions.

Sijin_Chandran
Active Contributor

Hello ABAP Gurus,

My query is regarding the below snapshot from help.sap.com for READ Table and the SY-SUBRC.

With the information for SY-SUBRC EQ '8' , I could conclude that even if the record is found and it was determined because of Binary Search then also SY-SUBRC could be set to '8'. Then why it's mentioned here 'Like sy-subrc equals 4' ? SY-SUBRC '4' is for Row not found and how we can relate these two in such cases. Please correct if I am wrong here.

Now I will highlight my below case, even though am not making use of SY_SUBRC for handling my case and using cx_sy_itab_line_not_found instead for safer side.

I have an SORTED Internal Table 'T_PROXYID_NR' with Key VBELN

Now for the below READ query, Data is present in the above Internal Table and it is successfully hit as well, then why it's SY-SUBRC '8' is my question. <LFS_FILE_DATA>-VBELN is 8000256664 here.

Does this mean that SY-SUBRC '8' is for denoting that for cases of SORTED Internal Table Binary Search was applied implicitly and the Record has been successfully retrieved(just like SY-SUBRC 0)?

I searched for this and the information many have mentioned is SY-SUBRC 8 means Binary Search failed because of wrong search criteria. But here everything is fine.

Helpful comments much appreciated.

Thanks,

Sijin

1 ACCEPTED SOLUTION

horst_keller
Product and Topic Expert
Product and Topic Expert

Unfortunately, the (translation of) documentation is wrong here.

It must be

8 Line was not found. The entry was searched using a binary search and the end of the table was reached. sy-tabix is set to the number of lines + 1.

The value of 8 points out that sy-tabix is set diefferently than for sy-subrc 4.

DATA itab TYPE STANDARD TABLE OF i
               WITH EMPTY KEY
               WITH UNIQUE SORTED KEY skey COMPONENTS table_line.

itab = VALUE #( FOR i = 9 THEN i - 2 UNTIL i < 0 ( i ) ).

READ TABLE itab WITH KEY table_line = 10
                         TRANSPORTING NO FIELDS.
                         "sy-subrc = 4, sy-tabix = 0

READ TABLE itab WITH KEY skey COMPONENTS table_line = 10
                         TRANSPORTING NO FIELDS.
                         "sy-subrc = 8, sy-tabix = 6
16 REPLIES 16

Sandra_Rossi
Active Contributor
0 Kudos

sy-subrc <> 0 means that the line is NOT FOUND, whatever it's 4 or 8. Why do you say it's found? If sorted access and sy-subrc <> 0, sy-tabix indicates the line before or after.

Note that your question is about the Table Expressions, but you show the help about READ TABLE. The table expressions don't set sy-subrc and sy-tabix. See the help about Table Expressions.

Sijin_Chandran
Active Contributor
0 Kudos

Hi Sandra,

Please find the comments inline,

- sy-subrc <> 0 means that the line is NOT FOUND, whatever it's 4 or 8. Why do you say it's found? If sorted access and sy-subrc <> 0, sy-tabix indicates the line before or after.

Why I said it's found is because I could see the Work Area getting filled correctly!. Line is found that's the reason even Exception is not caught.

-Note that your question is about the Table Expressions, but you show the help about READ TABLE. The table expressions don't set sy-subrc and sy-tabix. See the help about Table Expressions.

Alright! then let me check this my bad. If this is the case then this whole thread doesn't makes sense. I shall check whether there is any mention about SY-SUBRC over the help.sap.com page for Table Expressions.

I have edited the question and now added Table Expression in my subject as well. With the help.sap.com for Table Expressions I could make it that we just need to check the Exception for this case and should not bother about SY-SUBRC when Table Expression is there in place. Now my query just boils down to what this SY-SUBRC in this case denotes? does this denote Binary Search was performed Implicitly ?

Thanks,

Sijin

Sandra_Rossi
Active Contributor

In fact, "why do say it's found" was about your first sentence "With the information for SY-SUBRC EQ '8' , I could conclude that even if the record is found...", I was thinking that you were using READ TABLE...

Sorry, just got confused.

Sijin_Chandran
Active Contributor
0 Kudos

Based on your inputs I have changed the Subject and mentioned Table Expressions.

For me 'Table Expression' and 'READ Table' are like identical twins always 😄

Sandra_Rossi
Active Contributor

Not "are", you mean "were", hopefully.

See doc for table_exp - Table Expressions. The only case where sy-subrc can be set to something is when you use ASSIGN itab[ ... ] TO <fs>, and in that case it can be only 0 or 4 (not 8). In other case sy-subrc is not set (like your case). sy-tabix is never set.

Sijin_Chandran
Active Contributor
0 Kudos

Yes from this onwards "were" 🙂

touzik_itc
Active Participant
0 Kudos

READ TABLE and table expression itab[ itab_line ] are not identical. READ TABLE sets sy-subrc, if a table line found, and table expression not.

horst_keller
Product and Topic Expert
Product and Topic Expert

Unfortunately, the (translation of) documentation is wrong here.

It must be

8 Line was not found. The entry was searched using a binary search and the end of the table was reached. sy-tabix is set to the number of lines + 1.

The value of 8 points out that sy-tabix is set diefferently than for sy-subrc 4.

DATA itab TYPE STANDARD TABLE OF i
               WITH EMPTY KEY
               WITH UNIQUE SORTED KEY skey COMPONENTS table_line.

itab = VALUE #( FOR i = 9 THEN i - 2 UNTIL i < 0 ( i ) ).

READ TABLE itab WITH KEY table_line = 10
                         TRANSPORTING NO FIELDS.
                         "sy-subrc = 4, sy-tabix = 0

READ TABLE itab WITH KEY skey COMPONENTS table_line = 10
                         TRANSPORTING NO FIELDS.
                         "sy-subrc = 8, sy-tabix = 6

0 Kudos

Hi Horst

I had the following test:

read table itab with key table_line = 10<br>                         TRANSPORTING NO FIELDS<br>                         BINARY SEARCH.<br>cl_demo_output=>write_text( |SY-SUBRC : { sy-subrc } // SY-TABIX : { sy-tabix }| ).<br>cl_demo_output=>display( ).

obviously it is the same result has the SORTED KEY.

But, I thought the BINARY SEARCH is dichotomy search.

For me SAP should proceed as:

--> Read the middle of the table --> value 5 --> value search is greater than 10 ? no So

--> Read the middle of the upper remaining part of the table --> value 7 --> ...

--> Read the middle of the upper remaining part of the table --> value 9 --> ...

--> Nothing more exit

So I have done 4 access to the table, not 5

Or maybe it is table line number + 1 ..

horst_keller
Product and Topic Expert
Product and Topic Expert

Hi Frederic, sy-tabix does not show the number of table accesses, but simply the number off table lines + 1 in that case.

0 Kudos

Hi Horst,

I made a mistake here, which even sandra.rossi also pointed out.

My ask was with respect to Table Expression, I wrongly posted the documentation of READ Table here.

Now please guide on what SY-SUBRC 8 denotes for TABLE Expression applied over a SORTED Internal Table.

My assumption is it denotes "Binary Search was implicitly applied since the Table is a SORTED one, irrespective of record being found or not". Why I am saying irrespective of record found or not is because in my case the record was READ properly using Table Expression and the Exception was not caught.

Please correct me if I am wrong.

I think the answer you have made here with respect to READ Table holds well for Table Expression as well.

Thanks,

Sijin

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi,

table expression do not set sy-subrc. You must not evaluate sy-subrc after table expression.

Horst

0 Kudos

Hi,

First very thanks for responding.

As you had suggested and also in my code as well am making use of CATCH Exception only for handling the failure cases. But just wanted to know the reasons for SY-SUBRC getting SET to 8 when A Table Expression is executed on a Sorted Table.

Does that hint that Binary Search was applied Internally, there is no official documentation on this.

Because I must say still many people refrain from using Table Expressions saying that we cannot have Binary Search option there, but I have been trying to advocate that if we make proper Internal Table declarations BINARY SEARCH would be automatically taken care or.

Thanks,

Sijin

0 Kudos
sijin.chandran

Again, as Horst said, SY-SUBRC is not set for a Table Expression. I guess that if you see SY-SUBRC is 8 right after Table Expression, it's because it was already 8 right before the Table Expression is executed. SAP don't say that it's changed to undefined value either. If it's set to 8, you may contact SAP support.

Most of the time, I don't indicate the words "read table ... binary search" because I use Sorted Tables (or Hashed Tables). It's the same for Table Expressions. Only old-school developers use Standard Tables for finding one line. You may even use secondary indexes if you can't avoid Standard Tables, or even "auxiliary indexed tables" if you can't indicate secondary indexes.

The ABAP documentation explains that well.

0 Kudos

Alright, closing this thread.

But I must say the discussion here was a good one and informative for me 🙂

Thanks,

Sijin

Sandra_Rossi
Active Contributor
0 Kudos

NB: your screenshot comes possibly from 7.51 -> https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-US/index.htm?file=abapread_table.htm

(more recent help is a little bit different)