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: 

Loop Where

Former Member
0 Kudos

Hi,

I am trying to use a where condition in my loop statement as follows:

LOOP AT lt_sales_orders WHERE sales_org = l_salesorg AND

division = l_division AND

distr_chan = l_distchan AND

sales_off = l_salesoff.

Here, l_salesorg, l_division, l_distchan, l_salesoff are <b>optional</b> input parameters. This query works well when all these input parameters are non-empty.

But if the user does not enter anything in any of the parameters, it tries to match it with a blank entry while I dont want that. Rather I just want to exclude the check for that parameter from my where clause. I know that we can use the 'if else elseif' statement to form a where clause.

But is there a better way of doing it?

( I cannot use the 'LIKE' operator )

Thanks and Regards,

Reena

1 ACCEPTED SOLUTION

Former Member
0 Kudos

hi Reena,

You cannot put a check in your where clause (to check if it is intial, then skip this field from where condition).

Yes, as you mentioned, the option is to do if..elseif..condition check to

frame the where clause...i dont see any other options...

Hope this helps,

Sajan Joseph.

15 REPLIES 15

Former Member
0 Kudos

before loop u can put the

so and so variable and ..if not initial

then loop statement...

endloop.

endif....

regards

vijay pawar

Former Member
0 Kudos

Hello Reena,

Make the change like this:


LOOP AT lt_sales_orders WHERE sales_org IN l_salesorg AND
division IN l_division AND
distr_chan IN l_distchan AND
sales_off IN l_salesoff.

If the input fields  L_salesorg are parameter fields then built range table and use IN operator.


Hope this solve ur problem.

Vasanth

0 Kudos

Hi Vasanth,

Thanks for your reply.

Using 'IN' operator still tries to match the blank value. Didnt help

DATA: r_division TYPE RANGE OF bapiorders-division,

r_division_line LIKE LINE OF r_division,

r_division_line-sign = 'I'.

r_division_line-option = 'BT'.

r_division_line-low = l_division.

r_division_line-high = l_division.

And in the loop i used:

division in r_division

Any other inputs?

Thanks and Regards,

Reena

0 Kudos

Hello Reena,

When u r builting the RANGE make this change


DATA: r_division TYPE RANGE OF bapiorders-division,
r_division_line LIKE LINE OF r_division,
if not l_division is initial.  "  Add this line
r_division_line-sign = 'I'.
r_division_line-option = 'EQ'.
r_division_line-low = l_division.
*r_division_line-high = l_division. "Remove this
Append r_division
endif.

Hope this will solve ur problem.

Thanks !!

Vasanth

Former Member
0 Kudos

Hi,

Declare l_salesorg, l_division, l_distchan, l_salesoff as Select-options instead of paramters. If you want you can give as No extenstion for all of them.

The once data is fetched into Internal table lt_sales_orders, then use the following Statements:

Delete lt_sales_orders where NOT sales_org IN l_salesorg.

Delete lt_sales_orders where NOT division IN l_division.

Delete lt_sales_orders where NOT distchanIN l_distchan.

Delete lt_sales_orders where NOT sales_off IN l_salesoff.

Regards,

Anji

0 Kudos

Hi,

I cannot use 'Select-Options' since this is an RFC and the input data is coming from an external application.

I have to use import paramters.

Thanks and Regards,

Reena

0 Kudos

U do one thing... use 'IN' and also include the condition all parameters are not initial..

here is example code:

Loop at itab where x in p_x

and y in p_y

and z in p_z

and ( x is not initial or

y is not initial or

z is not initial ) ... in case you wanted the loop even if one of the parameters are not empty. Otherwise repalce the 'OR''s in the loop with AND.

Hope u got the point.

Former Member
0 Kudos

hi Reena,

You cannot put a check in your where clause (to check if it is intial, then skip this field from where condition).

Yes, as you mentioned, the option is to do if..elseif..condition check to

frame the where clause...i dont see any other options...

Hope this helps,

Sajan Joseph.

former_member404244
Active Contributor
0 Kudos

Hi,

make parameters as select-options and then delete

Delete lt_sales_orders where NOT sales_org IN l_salesorg .

Delete lt_sales_orders where NOT division IN l_division.

Delete lt_sales_orders where NOT distchanIN l_distchan.

Delete lt_sales_orders where NOT sales_off IN l_salesoff.

regards,

nagaraj

Former Member
0 Kudos

LOOP AT lt_sales_orders WHERE sales_org = l_salesorg AND

division = l_division AND

distr_chan = l_distchan AND

sales_off = l_salesoff and

sales_org ne space and

division ne space and

distr_chan ne space and

sales_off ne space.

Former Member
0 Kudos

Hi,

Replace your parameters with 'select-options' on selection screen in the following way:

SELECT-OPTIONS l_val FOR ... NO INTERVALS NO-EXTENSION.

From the user point of view they will act similarily to ordinary parameters but in the code you can use 'IN' operator..

LOOP AT ... WHERE VAL IN L_VAL....

Former Member
0 Kudos

Hi Prabhakar,

Try this ..........

LOOP AT lt_sales_orders WHERE

<b>l_salesorg IS NOT INITIAL AND </b>

<b>l_division IS NOT INITIAL AND </b>

<b>l_distchan IS NOT INITIAL AND </b>

<b>l_salesoff IS NOT INITIAL AND </b>

sales_org = l_salesorg AND

division = l_division AND

distr_chan = l_distchan AND

sales_off = l_salesoff AND.

Hope,your question is answered.If you have any queries, you are welocome

Reward,if helpful to you.

Regards,

V.Raghavender.

Former Member
0 Kudos

Hi,

Try like this, i hope you will get the solution.

LOOP AT lt_sales_orders WHERE

(sales_org = l_salesorg OR sales_org IS INITIAL)

AND (division = l_division OR division IS INITIAL)

AND (distr_chan = l_distchan OR distr_chan IS INITIAL) AND

sales_off = l_salesoff OR sales_off IS INITIAL).

Regards,

Sandhya

Former Member
0 Kudos

Hi ,

See this is not required when u are dealing with sales area .

loop at where ... <cond1> ...<cond2>..<cond3> ...<cond4>

In the select itself u can make the sales area org/dit chnl/division in the where clause .

If u r using with some sequence or a criteria

sort itab by org dischl division offfice
loop .

read table jtab with key salorg = itab-salsorg
                                   dischl = itab-distchnnl
                                   division = itab-div  
                                   office    = '1001'.

if sy-subrc eq 0.
process operation.


endif.
 
endloop.

in this u can use a case of fixed values also in place of itab .

replace itab- area with ur parameter values and check .

try this .

regards,

Vijay

Former Member
0 Kudos

Thanks a ton for all your inputs.

However, I was not able to use any of the solutions mentioned. I ended up using 'if' 'elseif' statements.

Thanks again !

Regards,

Reena