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

Condition WHERE in LOOP

Former Member
0 Likes
6,560

Good afternoon,

I have the following issue,

I have that do a LOOP to a table Internal, ie.

Loop at it_tabla where lgart = '0100' or lgart = '0200' or lgart = '0300' or lgart = '0400' or lgart = '0500' or lgart = '0600' or lgart = '0700' or lgart = '0800' .

.....

Endloop.

The question is, Is there any way , in the which not have that do many or in the sentence LOOP?

Kind regards.

1 ACCEPTED SOLUTION
Read only

former_member585060
Active Contributor
0 Likes
5,025

Hi,

Instead of where condition in LOOP and ENDLOOP, while fetching the data from the SAP table give the corresponding values in the WHERE condition.

SELECT * FROM <table> INTO TABLE it_tabla 
                WHERE lgart IN ( '0100', '0200', '0300', '0400', '0500', '0600', '0700', '0800'
                AND " other where conditions from selection screen.

now the internal table has only the values of required lgart.

Regards

Bala Krishna

11 REPLIES 11
Read only

Former Member
0 Likes
5,025

loop at it_tabla.

case it_tabla-lgart.

when '0100'

or '0200'

....

....

<stms>

endcase.

endloop.

Edited by: BrightSide on Mar 14, 2009 11:22 PM

Read only

Former Member
0 Likes
5,025

use

loop at it_tabla where lgart in ('0100' , '0200' , '0300' , '0400' , '0500' , '0600' , '0700' , '0800' )

using loop without where and using case can result in more iterations than required so can be performance issue.

кu03B1ятu03B9к

Read only

0 Likes
5,025

>

> using loop without where and using case can result in more iterations than required so can be performance issue.

кu03B1ятu03B9к - not really. It will go through the loop exactly once in both cases.

Rob

Read only

0 Likes
5,025

hi rob,

i did not understand why it will loop only once. Can u explain it please.

Read only

matt
Active Contributor
0 Likes
5,025

I think Rob means that it will go through the table once.

It's true for Standard tables. But as it says in the help on the WHERE clause.

While with standard tables all rows of the internal table are checked for the logical expression of the WHERE- addition, with sorted tables and hash tables (as of Release 7.0) you can achieve optimized access by checking that at least the beginning part of the table key in sorted tables and the entire table key in hash tables is equal in the logical expression through queries linked with AND. Optimization also takes effect if the logical expression contains other queries linked with AND with arbitrary operators

In this case though, the queries are linked with OR - so you still get all rows checked!

Now, if it_tabla is a sorted table with key lgart, then you could do the following:

PERFORM read_the_table USING: '0100', '0200',...

FORM read_the_table USING i_lgart TYPE ...

  data: l_tabix TYPE sytabix.

  READ TABLE it_table WITH TABLE KEY lgart =i_lgart TRANSPORTING NO FIELDS.
  l_tabix = sy-tabix.
  LOOP AT it_table FROM l_tabix INTO ls_wa.
    IF ls_wa-lgart NE i_lgart.
      EXIT.
    ENDIF.
    " Do stuff
  ENDLOOP.

ENDFORM.

matt

Read only

0 Likes
5,025

I will agree with both Rob and Matt. If you process the loop without giving the where and use the concept of a parallel cursor, you will be able to achieve your objective. Moreover, it will be much faster than your where condition.


*sort both tables on the key field (in this case field1)
loop at table1.
  loop at table2 from index v_tabix.
    if table2-field1 ne table1-field1.
      v_tabix = sy-tabix.
      exit.
    endif.
  endloop.
*  your code goes here.
endloop.

Read only

0 Likes
5,025

i think the above problem is not about nested loops so parallel cursor concept doesnot apply, and if loop inside loop is required, best approch is to use read table inside loop it would be better then even parallel cursor isn't it?

кu03B1ятu03B9к

Read only

0 Likes
5,025

>

> i think the above problem is not about nested loops so parallel cursor concept doesnot apply, and if loop inside loop is required, best approch is to use read table inside loop it would be better then even parallel cursor isn't it?

Not sure exactly what the question is, but what I was trying to say that using a bunch of ORs or a single IN will not speed up processing (as Matt said for standard tables only). Each record of the table will be read and evaluated in both cases.

Rob

Read only

former_member585060
Active Contributor
0 Likes
5,026

Hi,

Instead of where condition in LOOP and ENDLOOP, while fetching the data from the SAP table give the corresponding values in the WHERE condition.

SELECT * FROM <table> INTO TABLE it_tabla 
                WHERE lgart IN ( '0100', '0200', '0300', '0400', '0500', '0600', '0700', '0800'
                AND " other where conditions from selection screen.

now the internal table has only the values of required lgart.

Regards

Bala Krishna

Read only

0 Likes
5,025

Thank You for all.

I tested

loop at it_tabla where lgart in ('0100' , '0200' , '0300' , '0400' , '0500' , '0600' , '0700' , '0800' )

....

endloop.

But the system show an error:

Relational operator "IN" is not supported.

Kind Regards,

Sanew2

Read only

0 Likes
5,025

Hi,

ranges: gv_vbeln for vbak-vbeln.
gv_vbeln-low = '0000000047'.
gv_vbeln-option = 'EQ'.
gv_vbeln-sign = 'I'.
append gv_vbeln.

gv_vbeln-low = '0000000048'.
gv_vbeln-option = 'EQ'.
gv_vbeln-sign = 'I'.
append gv_vbeln.

*  loop at gt_final where vbeln in ( '0000000047' '0000000048' ).  " Failed
  loop at gt_final where vbeln in gv_vbeln.  " Worked fine
     write: gt_final-vbeln.
  endloop.

Above code will work surely, try it and let me know if find any difficulties.

regards

~Satya