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 statement giving problem

Former Member
0 Kudos

Dear Experts,

I am making monthly consumption report, In which i am getting monthly data column wise for different month , here firstly i am fetching header mblnr date according to date from mkpf then i am fetching data from mseg.....

I have same mblnr in both tables..... Now i am using read statement like this.......

sort lt_mkpf by mblnr.

SORT lt_mseg BY mblnr.

LOOP AT lt_mkpf INTO lw_mkpf.

READ TABLE lt_mseg INTO lw_mseg WITH KEY mblnr = lw_mkpf-mblnr

  • mjahr = lw_mkpf-mjahr

BINARY SEARCH.

When I am debugging i am getting data in lw_mkpf but sy-subrc = 4 is coming and getting wrong data in final table by using these above statements..........

where as if i am using loop like this:

SORT lt_mkpf BY mblnr.

SORT lt_mseg BY mblnr.

LOOP AT lt_mkpf INTO lw_mkpf.

v_mkpf = lw_mkpf-mblnr.

LOOP AT lt_mseg INTO lw_mseg WHERE mblnr EQ v_mkpf.

with this i am getting right data........ But it takes lot of time to execute..........

Can you please guide me am i using read statement by wrong method. which one is the correct method which one shoul be used.........

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

Try like this.... dont sort lt_mkpf table and then try


SORT lt_mseg BY mblnr.
LOOP AT lt_mkpf INTO lw_mkpf.
READ TABLE lt_mseg INTO lw_mseg WITH KEY mblnr = lw_mkpf-mblnr binary search.

Hope it will helps

7 REPLIES 7

Former Member
0 Kudos

Hi,

Try like this.... dont sort lt_mkpf table and then try


SORT lt_mseg BY mblnr.
LOOP AT lt_mkpf INTO lw_mkpf.
READ TABLE lt_mseg INTO lw_mseg WITH KEY mblnr = lw_mkpf-mblnr binary search.

Hope it will helps

Former Member
0 Kudos
READ TABLE lt_mseg INTO lw_mseg WITH KEY mblnr = lw_mkpf-mblnr binary search

here ur fetching only one record.

LOOP AT lt_mseg INTO lw_mseg WHERE mblnr EQ v_mkpf.

In this u may get no.of records.

SO continue with below statement only.

Regards,

Former Member
0 Kudos

Hi shelly,

As to say both are same but ,

In first you used ... year condition (mjahr.)

As to say year is not requried for mkpf to mseg.

mkpf is header and mseg is Item wise ....When ever user creats GR in MIGO .. automatically both tables will trigger ...

waiting for rewards...

From

Maddy .

Former Member
0 Kudos

Hi,

while Sorting your Table, Sort it with both MBLNR and MJAHR.

Sure this will solve your issue.

Refer the following SAP Help Content.

standard tables are subject to a linear search. If the addition BINARY SEARCH is specified, the search is binary instead of linear. This considerably reduces the runtime of the search for larger tables (from approximately 100 entries upwards). For the binary search, the table must be sorted by the specified search key in ascending order. Otherwise the search will not find the correct row.

Former Member
0 Kudos

Hey Dude, sorry my previous post was not presentable,

Refer the SAP help content now, from below.

standard tables are subject to a linear search. If the addition 
BINARY SEARCH is specified, the search is binary instead of 
linear. This considerably reduces the runtime of the search for 
larger tables (from approximately 100 entries upwards). For the
 binary search, the table must be sorted by the specified 
search key in ascending order. Otherwise the search will not
 find the correct row.

0 Kudos

Hello Shelly,

To solve the issue of read table being failed, Sort the internal table by mblnr and mjahr. say sort lt_mseg by mblnr mjahr but read the internal table mseg will give you only one data. What about the other line items. MSEG is material document line item table. So for one record in MKPF there can be more than one record in MSEG. So to consider all the line items, you need to loop on the MSEG internal table instead of read table.

LOOP AT lt_mkpf INTO lw_mkpf.

v_mkpf = lw_mkpf-mblnr.

LOOP AT lt_mseg INTO lw_mseg WHERE mblnr EQ v_mkpf.

This statement will take time and create performance issue. So rewrite the statement as

LOOP AT lt_mseg INTO lw_mseg.

read table lt_mkpf with key lw_mkpf binary search.

Regards

Farzan

0 Kudos

If the second one is working, try using the following.

LOOP AT lt_mkpf INTO lw_mkpf.

v_mkpf = lw_mkpf-mblnr.

READ TABLE lt_mseg INTO lw_mseg WITH KEY mblnr = v_mkpf.