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 read table with binary search

Former Member
0 Likes
6,145

hi

i am using the read table statement wth binary search but when i use with binary search as belwo a strange thing happens most oft he records get blank and the result of the report is not the same as that withouth using binary search

so where to use binary search and under which conditions

i am using

read table into wa_table

with key lclic = gt_itab-lclic binary search.

pls suggest when and how can we use binary search and any idea why the records are difrenet now thant the earlier and which is correct?

regards

Arora

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
3,255

Hi Arora,

Always use BINARY SEARCH with read statement and before using the binary search SORT the internal table.

in your case

SORT <table> BY lclic.

Reward points if useful.

Regards,

Atish

23 REPLIES 23
Read only

jayanthi_jayaraman
Active Contributor
0 Likes
3,255

Hi,

sort itab by lclic.

Before reading,sort it using the same key.

Read only

0 Likes
3,255

hi all

i am already sorting the table by the key each time but still the records are diffrent?

regards

arora

Read only

0 Likes
3,255

Hi,

I think you are sort inside the loop.You should use it outside.

sort itab by f1.

loop ....

clear wa.

read table itab into wa with key f1 = value binary search.

if sy-subrc eq 0.

endif.

...

endloop.

Message was edited by:

Jayanthi Jayaraman

Read only

0 Likes
3,255

Can you jsut paste your code here.

Regards,

Atish

Read only

0 Likes
3,255

here is the code

also i have problem reading the BPext statement from gt_but001

as guid_mobj is not key

and this is the select i am using for it

IF not gt_sagmeld[] is initial.

data: gt_sagmeld_temp like gt_sagmeld.

gt_sagmeld_temp[] = gt_sagmeld[].

sort gt_sagmeld_temp by guid_mobj.

delete adjacent duplicates from gt_sagmeld_temp[] comparing guid_mobj.

*

select /sapsll/corpar~parno

/sapsll/corpar~guid_mobj

but000~partner " pK

but000~bpext

into table gt_but001

from /sapsll/corpar

INNER join but000 on but000partner = /sapsll/corparparno

for all entries in gt_sagmeld_temp

where /sapsll/corpar~guid_mobj = gt_sagmeld_temp-guid_mobj

and /sapsll/corpar~PAFCT = 'SH'.

also let me know how to read bpext from gt_but001 correctly

sort gt_sagmeld by guid_lclic.

loop at gt_sagmeld into wa_sagmeld .

move wa_sagmeld-amnt_flt to gvsagmeldamntflt.

wa_sagmeld_outtab-amnt_flt = gvsagmeldamntflt.

read table gt_but001 into wa_but001

with key guid_mobj = wa_sagmeld-guid_mobj binary search.

wa_sagmeld_outtab-bpext = wa_but001-bpext.

wa_sagmeld_outtab-bpext = wa_but001-bpext. "

read table gt_sapsllcuit into wa_sapsllcuit

with key guid_cuit = wa_sagmeld-guid_pobj binary search .

...........

.........

Read table gt_corref into wa_corref with key

guid_pobj = wa_sapsllcuit-guid_cuhd binary search.

if sy-subrc = 0.

wa_sagmeld_outtab-refno = wa_corref-refno+20(20). "

endif.

Read table gt_prt into wa_prt with key

guid_pr = wa_sapsllcuit-guid_pr binary search .

if sy-subrc = 0.

gv_waprtxt = wa_prt-prtxt.

endif.

Read table gt_prcon into wa_prcon with key

guid_pr = wa_sapsllcuit-guid_pr binary search .

if sy-subrc = 0.

gv_prcon_segck = wa_prcon-secgk.

wa_sagmeld_outtab-secgk = gv_prcon_segck+0(3).

endif.

...........

loop at gt_ctsnumt into wa_ctsnumt

where guid_ctsnum = wa_corcts-guid_ctsnum and launr = '0001'.

Read table gt_ctsnumt into wa_ctsnumt1

With key guid_ctsnum = wa_corcts-guid_ctsnum

launr = '0002' binay search.

append wa_sagmeld_outtab to gt_sagmeld_outtab.

endloop.

IF SY-SUBRC NE 0.

Append wa_sagmeld_outtab to gt_sagmeld_outtab.

ENDIF.

clear wa_sagmeld_outtab.

endloop.

ENDFORM.

Read only

0 Likes
3,255

Hi,

Basically sort all the tables before loop.clear the workarea before reading.

sort gt_sagmeld by guid_lclic.

<b>sort gt_but001 by guid_mobj.

sort gt_sapsllcuit by guid_cuit .

sort gt_corref by guid_pobj.

sort gt_prt by guid_pr.

sort gt_prcon by guid_pr .</b>

loop at gt_sagmeld into wa_sagmeld .

move wa_sagmeld-amnt_flt to gvsagmeldamntflt.

wa_sagmeld_outtab-amnt_flt = gvsagmeldamntflt.

<b>clear wa_but001.</b>

read table gt_but001 into wa_but001

with key guid_mobj = wa_sagmeld-guid_mobj binary search.

wa_sagmeld_outtab-bpext = wa_but001-bpext.

wa_sagmeld_outtab-bpext = wa_but001-bpext. "

<b>

clear wa_sapsllcuit.</b>

read table gt_sapsllcuit into wa_sapsllcuit

with key guid_cuit = wa_sagmeld-guid_pobj binary search .

...........

.........

<b>clear gt_corref.</b>

Read table gt_corref into wa_corref with key

guid_pobj = wa_sapsllcuit-guid_cuhd binary search.

if sy-subrc = 0.

wa_sagmeld_outtab-refno = wa_corref-refno+20(20). "

endif.

<b>clear wa_prt.</b>

Read table gt_prt into wa_prt with key

guid_pr = wa_sapsllcuit-guid_pr binary search .

if sy-subrc = 0.

gv_waprtxt = wa_prt-prtxt.

endif.

clear wa_prcon.

Read table gt_prcon into wa_prcon with key

guid_pr = wa_sapsllcuit-guid_pr binary search .

if sy-subrc = 0.

gv_prcon_segck = wa_prcon-secgk.

wa_sagmeld_outtab-secgk = gv_prcon_segck+0(3).

endif.

...........

loop at gt_ctsnumt into wa_ctsnumt

where guid_ctsnum = wa_corcts-guid_ctsnum and launr = '0001'.

Read table gt_ctsnumt into wa_ctsnumt1

With key guid_ctsnum = wa_corcts-guid_ctsnum

launr = '0002' binay search.

append wa_sagmeld_outtab to gt_sagmeld_outtab.

endloop.

IF SY-SUBRC NE 0.

Append wa_sagmeld_outtab to gt_sagmeld_outtab.

ENDIF.

clear wa_sagmeld_outtab.

endloop.

Read only

0 Likes
3,255

Hi Arora,

You haven't sorted all your internal tables.Just SORT all of them and also check sy-subrc after read statement.

for e.g.

read table gt_but001 into wa_but001

with key guid_mobj = wa_sagmeld-guid_mobj binary search.

<b>if sy-subrc = 0.

...

endif.</b>

Reward all useful answers.

Regards,

Atish

Read only

0 Likes
3,255

let me try it and i will revert back and rewards points

also I HAVE also asked regarding the reading for the table gt_but001

i am not sure how to read the bpext from gt_but001

also thirdly

what diffrence does clear statment makes? and why there is necesary to sort it first also does hte statment i used give wrong data?

regards

nishant

Read only

0 Likes
3,255

Hi,

Let me answer regarding clear and SORT.

If you NOT clear the work area then next time you read the table then the work area will contain the old values. So for safer side clear the work area and the best practise is check sy-subrc.

SORT - Binary search work on the principle of SORT. So to work it properly only if you SORT the table.

and regarding gt_but001 I will just see if I can help.

Regards,

Atish

Read only

0 Likes
3,255

hi

i ahave made the changes as sort etc

but the below statement is giving error as there is loop insid the loop outer of sagmeld as u see in the code i pasted

the error is coming at on clearing gt_ctsnumt

the error msg is U MAY NOT DELETE OR OVEWRITE GT_CTSNUMT WITHIN A LOOP OVER ITSELF I SUPPOSE I HAVE TO PUT CLEAR OUTSIDE THE LOOP?

loop at gt_ctsnumt into wa_ctsnumt

where guid_ctsnum = wa_corcts-guid_ctsnum and launr = '0001'.

clear gt_ctsnumt.

Read table gt_ctsnumt into wa_ctsnumt1

With key guid_ctsnum = wa_corcts-guid_ctsnum

launr = '0002' binary search.

Concatenate wa_ctsnumt-text wa_ctsnumt1-text into gvctsnumtext.

wa_sagmeld_outtab-text = gvctsnumtext .

append wa_sagmeld_outtab to gt_sagmeld_outtab.

endloop.

IF SY-SUBRC NE 0.

Append wa_sagmeld_outtab to gt_sagmeld_outtab.

ENDIF.

Read only

0 Likes
3,255

Hi,

You should clear only workarea to avoid if there is any default values.

clear wa_ctsnumt1.

Read table gt_ctsnumt into wa_ctsnumt1...

Just now I had seen my reply.In one place, I have mistakenly cleared internal table.Only we need to clear workarea before reading.

clear <b>wa_corref</b>.

Read table gt_corref into wa_corref with key

guid_pobj = wa_sapsllcuit-guid_cuhd binary search.

if sy-subrc = 0.

wa_sagmeld_outtab-refno = wa_corref-refno+20(20). "

endif.

Message was edited by:

Jayanthi Jayaraman

Read only

0 Likes
3,255

HI ALL

YA NOW I AM GETTING THE SAME VALUE AS BEFORE

BUT PLEASE ELOBORATE MORE ON BINARY SEARCH OPTION LIKE WHAT DOES IT DO AN DHOW IT IMPORVES PERFORMANCE AND WHY SORTING IS NECCESSARY BEFORE IT

ALSO PLEASE ADDRESS MY BUT00 PROBLEM IF U WANT I CAN SEND THE CODE N SEPERATE MAIL AND DATABASE STRUCTURE I AM FACING SOME PROBLEM WITH A PERFRMANCE LOTS OF PROBLEMS AND I WILL REWARDS POINTS FOR ALL PROBLEMS SOLVED

REAGRDS

ARORA

Read only

0 Likes
3,255

ya Jayanthi

i already has rectified that mistake the errro was coming becasue of that only and some data also missed so i checked and got it already

but can you address on my but00 problem

my problem is to read bpext correctly as i have pasted the whole code

hi

consider a scenerio as below

int table in which data i already tehre gt_sagmeld (here primary key is guid_lclic)

and matching field for next select is guid_mobj

not corpar table is joined to butoo by parno.

and primary key of corpar is guid_corpar which has no link to gt_sagmeld

and primary key of but00 is partner and it isjoined to corpar by but00-partner = corpar-parno

adn i need to read but00-bpext

pls see the below seelct statment for it

gt_Saglemd has data...

data: gt_sagmeld_temp like gt_sagmeld.

gt_sagmeld_temp[] = gt_sagmeld[].

delete adjacent duplicates from gt_sagmeld_temp[] comparing guid_mobj

select corpar~PAFCT

corpar~parno

into table gt_corpar

from corpar

for all entries in gt_sagmeld_temp

where /sapsll/corpar~guid_mobj = gt_sagmeld_temp-guid_mobj

and /sapsll/corpar~PAFCT = 'SH'.

sort gt_corpar by parno.

delete adjacent duplicates from gt_corpar comparing parno.

select but000~partner

but000~bpext

from but000 into table gt_but001

*

for all entries in gt_corpar

where but000~partner = gt_corpar-parno.

now the table gt_but001 contans the required bpext

and i want to read it to outtab

reading...

loop at gt_satmeld into wa_gt_sagmeld

some field selection....

now how to read the bpext in this loop from but001

as there is table corpar also and then but001

read table gt_but000 into wa_but000

with key partner = corpar-parno

...but this will not work as firs i need to read corpar also so how to use the loop or what is procedure to read ...

pls suggest

Read only

0 Likes
3,255

Hi,

I am not sure whether I understood the requirement correctly.Anyway, I will try.

sort gt_corpar by guid_mobj.

sort gt_but000 by partner.

...

loop at gt_sagmeld into wa_sagmeld .

...

clear wa_corpar.

read table gt_corpar into wa_corpar

with key guid_mobj = wa_sagmeld-guid_mobj binary search.

if sy-subrc eq 0.

clear wa_but000.

read table gt_but000 into wa_but000

with key partner = wa_corpar-parno binary search.

wa_sagmeld_outtab-bpext = wa_but001-bpext.

endif.

Message was edited by:

Jayanthi Jayaraman

Read only

0 Likes
3,255

ya Jayanthi

i was also tried the same t hing to read table gt_but000

i suppose it work but not sure for correct entries

as

i have one doubt regarding the read table statment

1) can someone send me link to it so that i can undserstand fully

2) whne such case as i told above guid_mobj is not the primary key but it is the only link between the wa_sagmeld and corpar

and corpar here also parno. is the link to but000- partner

but again par no is not the primary key again in corpar table so will this statement hold good

also Jayanthi

if possible can u send me ur mail id as i am facing some performance problem and read problem for a report for some slect even thogh iam usning for all entries etc and usnign no joins or where clause etc but stll for 14000 entries the program is stuccking at the select statment we are using for corpar?

maily i want if anyone can givve me performance improvement tips

for eg here for a single guid_lclic in the data in table legcon for guid_pobj is abt 15000 and for this 15000 records our above record gets executed for corpar then we are selecting bpext from but000.

Read only

0 Likes
3,255

Hi,

If the key field which you are using for read stateemnt is not the primary key,then there is possiblity of more than one record for the condition.So in such a situation loop should use d with where condition for the table.

If you are using binary search for read statement after sorting the table based on the key field,the read statement will fetch the data faster.Basically for performance improvement, we use this method.

But if the condition which are passing is not a key field, then there are possiblities for more than one record.So loop should be use dwith where condition.

loop at itab into wa where f1 = value.

....

endloop.

Check this link or use f1 help for more information.

http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/read_t01.htm

Read only

0 Likes
3,255

hi jayanthi,

you want me to use the loop at the read statement the read statement which u give lastly is only to be used i am pasting he select statment as below

problem one is t he loop which you are saying so should i use the loop while reading as below the code so how will the read statement as below will be ?

i am pasting the select statement code below in that also i ma facing a problem in last select as the last two select statement are not executing and time out errror comes can u suggest on that also?

clear wa_corpar.

read table gt_corpar into wa_corpar

with key guid_mobj = wa_sagmeld-guid_mobj binary search.

if sy-subrc eq 0.

clear wa_but000.

read table gt_but000 into wa_but000

with key partner = wa_corpar-parno binary search.

wa_sagmeld_outtab-bpext = wa_but001-bpext.

endif.

consider the code below and the number of entries AND FIND COMMENT AGAINST EACH SELECT FIRST SELECTS IS OK AND

SELECT a~extno

a~guid_lclic " for next select but000

e~ctsim

e~ctsex

e~srvll

e~ctsty

e~lgreg

INTO TABLE gt_sagmeld

FROM /SAPSLL/LCLIC as a

INNER JOIN /sapsll/tlegsv as e on e~lgreg = a~lgreg

where a~extno in s_extno.

THIS IS OK AS THE NUMBER OF RECORDS IS Table[43x176] AND NOT ROBLEM IN EXECUTION FAST

sort gt_sagmeld by guid_lclic.

delete adjacent duplicates from gt_sagmeld comparing all fields.

IF not gt_sagmeld[] is initial.

SELECT /sapsll/legcon~guid_lclic

/sapsll/legcon~guid_pobj

/sapsll/legcon~amnt_flt

/sapsll/legcon~amcur

/sapsll/legcon~guid_mobj

INTO TABLE gt_but000

FROM /SAPSLL/LEGCON

for all entries in gt_sagmeld

where /SAPSLL/legcon~guid_lclic = gt_sagmeld-guid_lclic.

THIS ABOVE IS ALSO OK THOGH NOW THE RECORDS ARE MUCH MORE

Table[1022259x72]

IF NOT GT_BUT000[] IS INITIAL.

sort gt_but000 by guid_pobj.

delete adjacent duplicates from gt_but000 comparing guid_mobj.

*

select /sapsll/corpar~guid_mobj

/sapsll/corpar~PAFCT

/sapsll/corpar~parno

into table gt_BUT001

from /sapsll/corpar

for all entries in gt_but000

where /sapsll/corpar~guid_mobj = gt_but000-guid_mobj

and /sapsll/corpar~PAFCT = 'SH'.

*DELETE gt_but001 where PAFCT <> 'SH'. I TIRED TO REMOVE THE CONDITON FOR WHERE ALSO BUT THIS ABOVE STATEMENT IS HANGING UP

AND SO THE THE BELOW ALSO?

sort gt_BUT001 by parno.

delete adjacent duplicates from gt_BUT001 comparing parno.

select but000~partner

but000~bpext

from but000 into table gt_but002

for all entries in gt_BUT002

where but000~partner = gt_corpar-parno.

REAGARDS

Arora

Read only

0 Likes
3,255

Hi,

clear wa_corpar.

read table gt_corpar into wa_corpar

with key guid_mobj = wa_sagmeld-guid_mobj binary search.

if sy-subrc eq 0.

clear wa_but000.

read table gt_but000 into wa_but000

with key partner = wa_corpar-parno binary search.

wa_sagmeld_outtab-bpext = wa_but001-bpext.

endif.

If for every parno in gt_corpar if there exists more records in gt_butoo, then you should use loop.If not, you can use read.

clear wa_corpar.

read table gt_corpar into wa_corpar

with key guid_mobj = wa_sagmeld-guid_mobj binary search.

if sy-subrc eq 0.

clear wa_but000.

  • Make it loop as last in the other loop you are using

loop at gt_but000 into wa_but000

where partner = wa_corpar-parno binary search.

wa_sagmeld_outtab-bpext = wa_but001-bpext.

append wa_sagmeld_outtab to gt_sagmeld_outtab.

endloop.

if sy-subrc ne 0.

append wa_sagmeld_outtab to gt_sagmeld_outtab.

endif.

This is regarding select statement.If you are going to use for all entries using a itab, then that particular itab should not be empty.

IF NOT GT_BUT000[] IS INITIAL.

sort gt_but000 by guid_pobj.

delete adjacent duplicates from gt_but000 comparing guid_mobj.

*

select /sapsll/corpar~guid_mobj

/sapsll/corpar~PAFCT

/sapsll/corpar~parno

into table gt_BUT001

from /sapsll/corpar

for all entries in gt_but000

where /sapsll/corpar~guid_mobj = gt_but000-guid_mobj

and /sapsll/corpar~PAFCT = 'SH'.

Try to use more conditions as much as possible

sort gt_BUT001 by parno.

delete adjacent duplicates from gt_BUT001 comparing parno.

<b>if not gt_but002[] is initial.</b>

select but000~partner

but000~bpext

from but000 into table gt_but002

for all entries in gt_BUT002

where but000~partner = gt_corpar-parno.

<b>endif.</b>

If again time out is happening,try creating new session while this is running and do something in that session.This can make you to avoid time out error. Also you can try running in background.

Read only

0 Likes
3,255

hi jayanthi

i will come to read statement once i address this performacne problem

1)can u please eloborate more on the statement of create session and do sth i suupose i am not clear with it

and running in back ground i can do but there are lots of selects statements and most of them giving problem as the one i pasted above and

this is also one i ahve commented the inner join still the statement is taking hell lot of time to execute can u suggest on this

SELECT /sapsll/corcts~guid_ctsnum " next select

/sapsll/corcts~stcts " read statement

  • /sapsll/ctsnum~ccngn " to display

INTO table gt_corcts

FROM /sapsll/corcts

  • INNER JOIN /sapsll/ctsnum on /sapsll/ctsnumguid_ctsnum = /sapsll/corctsguid_ctsnum

FOR all entries in gt_sagmeld

WHERE /sapsll/corcts~stcts = gt_sagmeld-ctsex

  • * *revised spec 3rd

and /sapsll/corcts~guid_pobj = gt_Sagmeld-guid_pobj.

regards

nishant

Read only

0 Likes
3,255

Hi,

If the program is executed in background, you can atleast come to a conclusion that whether the output is correct.

If you want to avoid the time out,while the program is running,right click that window and create another session and do something in that to make the screen active.

Read only

Former Member
0 Likes
3,256

Hi Arora,

Always use BINARY SEARCH with read statement and before using the binary search SORT the internal table.

in your case

SORT <table> BY lclic.

Reward points if useful.

Regards,

Atish

Read only

Former Member
0 Likes
3,255

when you use read table with binary search

use sort command ,then use binary search

<b>sort wa_table by iclic.</b>

loop at itab.

read table into wa_table

with key lclic = gt_itab-lclic binary search.

if sy-subrc eq 0.

do the further operation

endif

<b>clear :wa_table</b>

endloop.

Thanks

Seshu

Read only

Former Member
0 Likes
3,255

Hi

Before using read, u should sort the internal table.

Change ur code into following code.

Read table gt_itab into wa_table with key lclic = wa_table-lclic binary search.

Reward me if its useful

Regards

Ravi