Application Development and Automation 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: 
Read only

Problem with the Read Statement

Former Member
0 Likes
831

Hi All,

I have one issue while using the read table .Iam using Read table with binary search .but even though there is matching key Sy-subrc is returning 4.Since I used the binary search there is no need of using sort table before read.

Can some one help me to solve this issue .

Thanks in Advance

-Padma

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
806

example

  • FETCHING NAME1

SELECT werks

name1

FROM t001w

INTO TABLE t_name1

FOR ALL ENTRIES IN t_itab1

WHERE werks = t_itab1-werks.

SORT t_itab1 BY werks.

SORT t_name1 BY werks.

IF sy-subrc = 0.

LOOP AT t_itab1 INTO wa_itab1.

hold_tabix = sy-tabix.

READ TABLE t_name1 INTO wa_name1 WITH KEY werks = wa_itab1-werks

BINARY SEARCH.

IF sy-subrc = 0.

wa_itab1-name1 = wa_name1-name1.

MODIFY t_itab1 INDEX hold_tabix FROM wa_itab1.

ENDIF.

8 REPLIES 8
Read only

Former Member
0 Likes
806

before binary search sorting should be done.

Read only

Former Member
0 Likes
806

Hi,

u need to sort the table before using BINARY SEARCH in the read statement.

cud u pls give us the code....

here is a sample:

sort i_final by BUKRS .

LOOP AT i_final INTO w_final.

READ TABLE i_output INTO w_output WITH KEY

bukrs = w_final-bukrs

BINARY SEARCH.

ENDLOOP.

Regards,

Anjali

Read only

Former Member
0 Likes
806

HI,

You must sort the table with key which u r using in Read statement before using Binary search.

Regards,

Balavardhan.K

Read only

Former Member
0 Likes
807

example

  • FETCHING NAME1

SELECT werks

name1

FROM t001w

INTO TABLE t_name1

FOR ALL ENTRIES IN t_itab1

WHERE werks = t_itab1-werks.

SORT t_itab1 BY werks.

SORT t_name1 BY werks.

IF sy-subrc = 0.

LOOP AT t_itab1 INTO wa_itab1.

hold_tabix = sy-tabix.

READ TABLE t_name1 INTO wa_name1 WITH KEY werks = wa_itab1-werks

BINARY SEARCH.

IF sy-subrc = 0.

wa_itab1-name1 = wa_name1-name1.

MODIFY t_itab1 INDEX hold_tabix FROM wa_itab1.

ENDIF.

Read only

Former Member
0 Likes
806

HI,

SORTING IS A PRE REQUISIT FOR BINARY SEARCH. BEFORE REAT TABLE ST SORT UR INTERNAL TABLE WITH A KEY FIELD AND TRY.

WITH REGARDS,

SURESH ALURI.

Read only

Former Member
0 Likes
806

can you paste ur code

Read only

Former Member
0 Likes
806

Thanks all !!

The problem has been resolved!

Read only

Former Member
0 Likes
806

Hi,

<u><b>Binary Search in Standard Tables</b></u>

If you read entries from standard tables using a key other than the default key, you

can use a binary search instead of the normal linear search. To do this, include the addition BINARY SEARCH in the corresponding READ statements.

READ TABLE <itab> WITH KEY = <f> <result> BINARY SEARCH.

and

READ TABLE <itab> WITH KEY <k1> = <f1> ... <kn> = <fn> <result>

BINARY SEARCH.

The standard table must be sorted in ascending order by the specified search key. The BINARY SEARCH addition means that you can access an entry in a standard table by its key as quickly as you would be able to in a sorted table.

Example

DATA: BEGIN OF LINE,

COL1 TYPE I,

COL2 TYPE I,

END OF LINE.

DATA ITAB LIKE STANDARD TABLE OF LINE.

DO 4 TIMES.

LINE-COL1 = SY-INDEX.

LINE-COL2 = SY-INDEX ** 2.

APPEND LINE TO ITAB.

ENDDO.

SORT ITAB BY COL2.

READ TABLE ITAB WITH KEY COL2 = 16 INTO LINE BINARY SEARCH.

WRITE: 'SY-SUBRC =', SY-SUBRC.

The output is:

SY-SUBRC = 0

The program fills a standard table with a list of square numbers and sorts them into

ascending order by field COL2. The READ statement uses a binary search to look

for and find the line in the table where COL2 has the value 16.

Regards,

Bhaskar