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: 

read table binary search

Former Member
0 Kudos

Hi all ,

This is my code .some how my read statement is not workng . Can any one tell me what will be the issue with read statement?

FORM delete_entries.

DATA:v_indx LIKE sy-tabix.

*Get delete materials from zwplccrimsr table

SORT t_split BY matnr zwatctgp.

LOOP AT t_zwplccrimsr_hash.

READ TABLE t_split WITH KEY matnr = t_zwplccrimsr_hash-matnr

zwatctgp = t_zwplccrimsr_hash-zwatctgp

BINARY SEARCH.

v_indx = sy-tabix .

IF sy-subrc <> 0.

MOVE-CORRESPONDING t_zwplccrimsr_hash TO t_split.

t_split-xloek = 'X'.

APPEND t_split.

CLEAR t_split.

ELSE.

IF t_split-zwstdt = t_zwplccrimsr_hash-zwstdt AND

t_split-zweddt = t_zwplccrimsr_hash-zweddt AND

t_split-xloek = ' '.

DELETE t_split INDEX v_indx .

ELSE.

t_split-zwcrus = t_zwplccrimsr_hash-zwcrus. "V001

t_split-zwcrdt = t_zwplccrimsr_hash-zwcrdt.

t_split-zwcrtm = t_zwplccrimsr_hash-zwcrtm.

t_split-zwchus = 'CREST'.

t_split-zwchdt = sy-datum.

t_split-zwchtm = sy-uzeit.

MODIFY t_split INDEX v_indx.

ENDIF.

ENDIF.

ENDLOOP.

ENDFORM. "delete_entries

LINE

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Uwe is correct, please move the sort statement inside the loop then your binary search will work. But this may result performance issue if your internal table is too large (> million) sorting in a loop. You can also do as Amey suggested which is to remove BINARY SEARCH but if your internal table has too many records, it will result in a performance issue.

5 REPLIES 5

uwe_schieferstein
Active Contributor
0 Kudos

Hello Priya

If you want to use BINARY SEARCH then you have to sort your itab as you did:

...
SORT t_split BY matnr zwatctgp.  " for BINARY SEARCH required
LOOP AT t_zwplccrimsr_hash.
  READ TABLE t_split 
            WITH KEY matnr = t_zwplccrimsr_hash-matnr
                        zwatctgp = t_zwplccrimsr_hash-zwatctgp
            BINARY SEARCH.
...

However, within the loop you append your itab which will most likely disrupt the sorting:

...
APPEND t_split.  " Still a sorted itab???
CLEAR t_split.
ELSE.
IF t_split-zwstdt = t_zwplccrimsr_hash-zwstdt AND
t_split-zweddt = t_zwplccrimsr_hash-zweddt AND
t_split-xloek = ' '.
DELETE t_split INDEX v_indx.  " should not disrupt sorting
ELSE..
...

Thus, either you sort your itab within the loop before the BINARY SEARCH or you collect the entries into an additional itab.

Regards

Uwe

Former Member
0 Kudos

If internal table is of type standard then remove BINARY SEARCH option, it will work.

Regards,

Amey

Former Member
0 Kudos

Uwe is correct, please move the sort statement inside the loop then your binary search will work. But this may result performance issue if your internal table is too large (> million) sorting in a loop. You can also do as Amey suggested which is to remove BINARY SEARCH but if your internal table has too many records, it will result in a performance issue.

0 Kudos

Internal table contains 1,65,000 records .If i remove Binary search is that causes any problem?

uwe_schieferstein
Active Contributor
0 Kudos

Hello Priya

If you need to add new entries to T_SPLIT within your loop then you could define this itab as <b>SORTED TABLE WITH UNIQUE KEY</b> <i>matnr zwatctgp </i>(I assume that this is possible. This means the itab cannot have duplicate entries for matnr zwatctgp ).

"SORT t_split BY matnr zwatctgp.  " obsolete if already SORTED TABLE
LOOP AT t_zwplccrimsr_hash.
READ TABLE t_split 
          WITH TABLE KEY matnr = t_zwplccrimsr_hash-matnr
                                zwatctgp = t_zwplccrimsr_hash-zwatctgp.

v_indx = sy-tabix .
IF sy-subrc <> 0.
MOVE-CORRESPONDING t_zwplccrimsr_hash TO t_split.
t_split-xloek = 'X'.
"APPEND t_split.
INSERT t_split INTO TABLE t_split.
CLEAR t_split.
ELSE.
IF t_split-zwstdt = t_zwplccrimsr_hash-zwstdt AND
t_split-zweddt = t_zwplccrimsr_hash-zweddt AND
t_split-xloek = ' '.
DELETE t_split INDEX v_indx .
ELSE.
t_split-zwcrus = t_zwplccrimsr_hash-zwcrus. "V001
t_split-zwcrdt = t_zwplccrimsr_hash-zwcrdt.
t_split-zwcrtm = t_zwplccrimsr_hash-zwcrtm.
t_split-zwchus = 'CREST'.
t_split-zwchdt = sy-datum.
t_split-zwchtm = sy-uzeit.

MODIFY t_split INDEX v_indx.
...

Regards

Uwe