‎2009 Jun 01 12:59 PM
Dear forumers,
I'm in the midst of coding a rather complex data retrieval logics for an ALV report.
In the report, I have 10 select-options (all are with ranges). At one point, I would like to filter the data in an internal table based on the select-option inputs there. All the other data retrievals from this point forward will be based on the filtered internal table (i.e i_tab4 in the codes below).
I've coded this segment in such a way, but there seems to be problems with the read statement here (I think it's rather impossible to code it this way as well because the compiler complained on a syntax error at the IN word used).
if i_tab3 is not initial.
read i_tab3 into w_wa_3
with key bukrs IN s_bukrs and
anln1 IN s_anln1 and
invnr IN s_invnr and
deakt IN s_deakt and
prctr IN s_prctr and
...
matnr IN s_matnr.
if sy-subrc = 0.
" Codes for moving the contents in w_wa_3 to w_wa_4 and finally to i_tab4
endif.
endif.Are there any better ways to filter data in an internal table? Please help (as I'm still a newbie). I'd really appreciate any inputs here at all.
Thanks.
‎2009 Jun 01 1:03 PM
‎2009 Jun 01 1:06 PM
Thanks, Raymond..
Actually, I'm a little hesitant to use the LOOP method (LOOP AT i_tab3 into w_wa_3 where <condition>) because it might just mean using a lot of loops with multiple conditions, thus affecting the program's performance.
What are your thoughts on this?
‎2009 Jun 01 1:06 PM
Hi,
You can use the Loop and Endloop statmenst to filter some date while you are read the table..
LOOP AT ITAB INTO WA
READ I_TAB3 INTO WA_TAB3 with KEY BUKRS = WA-BUKRS
ANLN = WA-ANLAN........
ENDLOOP.
Like above you can filter the data ver well.
Regards
Thiru
‎2009 Jun 01 1:12 PM
Thanks, Thirupathy..
The problem here is, all the 10 select-options have ranges and I need to filter my data in the internal table based on them.
As similar to coding a SELECT statement, I would code "SELECT ... WHERE BUKRS IN S_BUKRS ...". But, isn't "READ TABLE ... WITH KEY BUKRS = S_BUKRS" less accurate here (considering that the S_BUKRS is a range and not a single value)?
Please help.
‎2009 Jun 01 1:07 PM
it's not posible to use the IN while reading an internal table try somthing like following
if i_tab3 is not initial.
loop at i_tab3 into w_wa_3
where bukrs IN s_bukrs and
and anln1 IN s_anln1 and
and invnr IN s_invnr and
and deakt IN s_deakt and
and prctr IN s_prctr and
.
and matnr IN s_matnr.
endloop.
if sy-subrc = 0.
" Codes for moving the contents in w_wa_3 to w_wa_4 and finally to i_tab4
endif.
endif.
‎2009 Jun 01 1:09 PM
Code should look like
IF i_tab3 IS NOT INITIAL.
LOOP AT i_tab3 INTO w_wa_3
WHERE bukrs IN s_bukrs AND
anln1 IN s_anln1 AND
invnr IN s_invnr AND
deakt IN s_deakt AND
prctr IN s_prctr AND
...
matnr in s_matnr.
" Codes for moving the contents in w_wa_3 to w_wa_4 and finally to i_tab4
ENDLOOP.
ENDIF.- If only first record is required, put an EXIT statement in the LOOP/ENDLOOP block.
- Try to filter the database SELECT before filling the internal table
- if you have several level of LOOP, you may copy internal table, and delete record already processed
- The only optimization in LOOP WHERE comes when the internal table as a sorted type, and the where criteria are the first sequential keys
Regards,
Raymond
‎2009 Jun 01 1:08 PM
Hi,
Try like this.
delete itab3 where bukrs NOT IN s_bukrs or
anln1 NOT IN s_anln1 or
invnr NOT IN s_invnr or
deakt NOT IN s_deakt or
prctr NOT IN s_prctr or
...
matnr IN s_matnr.
Kiran
‎2009 Jun 01 1:09 PM
Hi,
At the Select statement level u can sort the data based on above condition.
select * from <table_name> into table i_tab3
where bukrs IN s_bukrs and
anln1 IN s_anln1 and
invnr IN s_invnr and
deakt IN s_deakt and
prctr IN s_prctr and
...
matnr IN s_matnr.
if sy-subrc is initial.
itab4[] = itab3[].
endif.
‎2009 Jun 01 1:10 PM
Hi Tan,
you can filter by this way.
loop at i_tab3 into w_wa_3 where bukrs IN s_bukrs and
anln1 IN s_anln1 and
invnr IN s_invnr and
deakt IN s_deakt and
prctr IN s_prctr and
...
matnr IN s_matnr.
endloop.
regards,
ravibabu.a
‎2009 Jun 01 1:10 PM
Hi,
Just practice as below,
DELETE i_tab3 WHERE bukrs IN s_bukrs.
DELETE i_tab3 WHERE anln1 IN s_anln1. etc......
Regards,
Raju.
‎2009 Jun 01 1:20 PM
Hi ,
if i_tab3 is not initial.
read i_tab3 into w_wa_3
with key bukrs IN s_bukrs and
anln1 IN s_anln1 and
invnr IN s_invnr and
deakt IN s_deakt and
prctr IN s_prctr and
...
matnr IN s_matnr.
if sy-subrc = 0.
" Codes for moving the contents in w_wa_3 to w_wa_4 and finally to i_tab4
endif.
endif.
For this you can try logic as below mention.
append all ranges at selection screen output. then sort this by appropriate field and then use read statement for filtering data.
Hope this is useful for you.
Regards,
Vijay
‎2009 Jun 01 1:35 PM
A huge thanks to everyone, for the generous inputs provided here.. I really do appreciate it a lot..
GTREN, Kiran, Ravibabu, I'll definitely consider your suggestions there..
Raymond, thanks for the good tips and advice given.. Really appreciate it as well..
- If only first record is required, put an EXIT statement in the LOOP/ENDLOOP block.
--> All the records are needed, not just the first
- Try to filter the database SELECT before filling the internal table
--> This is done already
- if you have several level of LOOP, you may copy internal table, and delete record already processed
--> Will probably do this too
- The only optimization in LOOP WHERE comes when the internal table as a sorted type, and the where criteria are the first sequential keys
--> Will take note of it
Usha, unfortunately, there are about 7 database tables to select data from, and it isn't as simple / straightforward to just do the data retrieval in one select statement. I have however, filtered the data accordingly in my earlier SELECT statements too.
S Raju, I'll also consider your suggestions, but perhaps by using just one delete statement with multiple conditions in it.
Vijay, this is an interesting way too, will definitely consider your suggestion here as well..
‎2009 Jun 05 9:19 AM
Just an update here..
After much tests, I found that the following method works the fastest (and most straight-forward) too:-
DELETE i_tab3
WHERE bukrs NOT IN s_bukrs.
DELETE i_tab3
WHERE anln1 NOT IN s_anln1.
DELETE i_tab3
WHERE invnr NOT IN s_invnr.
DELETE i_tab3
WHERE prctr NOT IN s_prctr.
DELETE i_tab3
WHERE matnr NOT IN s_matnr.
...Thanks once again for all the help and inputs given!