‎2005 Dec 19 7:22 PM
Hello ABAP Experts,
I have a internal i_gl_account table which has some of the following fields:
glacctfrom, glacctto, flag
i have to select a record from this internal table based on a fixed gl_account_fix.
i cannot write this statement
read table i_gl_account with key
glacctfrom >= gl_account_fix
glacctto <= gl_account_fix
since read does not allow the '>' or '<' operators.
one alternative possible is:
loop i_gl_account.
if ( glacctfrom >= gl_account_fix and
glacctto <= gl_account_fix).
.....
.....
exit.
endif.
endloop.
This solutions, lookups at each and every record one by one and then does the if comparisions. Incase the i_gl_account is large then this is huge cost and processing.
wondering if there is any other possible method of achieving the same with lesser processing and cost.
Any suggetions are appreciated.
Thanks,
BWer
‎2005 Dec 19 7:24 PM
‎2005 Dec 19 7:24 PM
‎2005 Dec 19 7:24 PM
Hi,
You can use as
LOOP AT i_gl_account WHERE glacctfrom >= gl_account_fix
and glacctto <= gl_account_fix.
ENDLOOP.
‎2005 Dec 19 7:26 PM
Hi BWer,
You can use the where claus einstead of the IF statement.
loop i_gl_account where
( glacctfrom >= gl_account_fix and
glacctto <= gl_account_fix).
.....
.....
exit.
endloop.
Regards,
Suresh Datti
‎2005 Dec 19 7:26 PM
Hi
Put the condition in the where clause of the loop stmt.
loop at i_gl_account where
glacctfrom >= gl_account_fix and
glacctto <= gl_account_fix .
...........
endloop.
- Kalpana
‎2005 Dec 19 7:37 PM
You could also something simular to this sample program. If know that the start point is in the table. Here the program is reading the first line where the start value is =, then it is reading each line after that untill it reaches the end point. But it is not reading the entire internal table.
report zrich_0001.
data: begin of itab occurs 0,
field1(10) type c,
field2(10) type c,
field3(10) type c,
end of itab.
itab-field1 = 'A'.
itab-field2 = '1'.
itab-field3 = '001'.
append itab.
itab-field1 = 'A'.
itab-field2 = '2'.
itab-field3 = '123'.
append itab.
itab-field1 = 'A'.
itab-field2 = '3'.
itab-field3 = '124'.
append itab.
itab-field1 = 'A'.
itab-field2 = '4'.
itab-field3 = '125'.
append itab.
itab-field1 = 'A'.
itab-field2 = '5'.
itab-field3 = '999'.
append itab.
sort itab ascending by field1 field2 field3.
data: index type sy-index.
data: start like itab-field3 value '123'.
data: end like itab-field3 value '125'.
read table itab with key field3 = start.
if sy-subrc = 0.
write:/ itab-field1, itab-field2, itab-field3.
index = sy-tabix + 1.
while sy-subrc = 0.
read table itab index index.
if sy-subrc = 0
and itab-field3 <= end.
write:/ itab-field1, itab-field2, itab-field3.
else.
sy-subrc = 4.
endif.
index = index + 1.
endwhile.
endif.
Regards,
Rich Heilman
‎2005 Dec 19 8:41 PM
Hi BWer,
If a standard internal table is already sorted and you perform a binary search on it for a <b>non-existent</b> key value, the SY_TABIX always point to the index <u><b>where that key value can be inserted while keeping the table sorted</b></u>. You can use this property to write very simple and very fast program. Please see the following example. Please copy and test this example.
report zsharad.
data :begin of l_tab ,
n type i,
end of l_tab.
data : i_tab like table of l_tab with header line.
*-----Selection screen
parameters: p_st type I, "Start value
p_en type I. "End value
*-------Main program
start-of-selection.
*-----Prepare internal table
i_tab-n = '3'.
append i_tab.
i_tab-n = '5'.
append i_tab.
i_tab-n = '6'.
append i_tab.
i_tab-n = '8'.
append i_tab.
i_tab-n = '9'.
append i_tab.
i_tab-n = '12'.
append i_tab.
i_tab-n = '16'.
append i_tab.
i_tab-n = '20'.
append i_tab.
sort i_tab.
*----Get start index by reading table by starting value
read table i_tab with key n = p_st
binary search.
loop at i_tab from sy-tabix.
*-----Keep on reading till end of the table or value becomes greater
*-----than end value
if i_tab-n GT p_en.
exit.
endif.
write :/01 i_tab-n.
endloop.