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

internal table - select if in range ?

Former Member
0 Likes
723

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

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
697

Have you tried the WHERE clause for the LOOP.




loop i_gl_account.
   where glacctfrom >= gl_account_fix
     and   glacctto <= gl_account_fix.
.....
exit.
endloop.



Regards,

Rich Heilman

6 REPLIES 6
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
698

Have you tried the WHERE clause for the LOOP.




loop i_gl_account.
   where glacctfrom >= gl_account_fix
     and   glacctto <= gl_account_fix.
.....
exit.
endloop.



Regards,

Rich Heilman

Read only

Former Member
0 Likes
697

Hi,

You can use as

LOOP AT i_gl_account WHERE glacctfrom >= gl_account_fix

and glacctto <= gl_account_fix.

ENDLOOP.

Read only

suresh_datti
Active Contributor
0 Likes
697

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

Read only

Former Member
0 Likes
697

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

Read only

0 Likes
697

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

Read only

Former Member
0 Likes
697

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.