‎2013 Nov 08 6:37 AM
after fetching the data to it_mseg ,
i have declared one parameter for matnr in selection screen.
after fetching all records i want to delet all materials which dont match with pr_matnr .
how to write this statement.
‎2013 Nov 08 6:40 AM
Hi,
You can write it as below:
DELETE it_mseg WHERE matnr NE pr_matnr.
Thanks & Regards
Priyesh Shah
‎2013 Nov 08 6:42 AM
‎2013 Nov 08 6:42 AM
‎2013 Nov 08 6:44 AM
Hi Manish,
You don't need to write it in Loop's.
Writing DELETE statement in LOOP ... END LOOP is not a good programming Practice.
Thanks & Regards
Priyesh Shah.
‎2013 Nov 08 11:50 AM
‎2013 Nov 08 6:43 AM
I think you can put the NE in the selection statement itself where you fetch this material...
‎2013 Nov 08 6:46 AM
‎2013 Nov 08 6:45 AM
Hello,
Did you try this?
Loop into ur itab(it_mseg) into wa_mseg.
if wa_msg-matnr ne pr_matnr.
DELETE it_mseg WHERE matnr ne pr_matnr.
endif.
endloop.
‎2013 Nov 08 6:48 AM
Hi Manisha,
This is a bad programming practice.
Do not delete any thing inside the LOOP...ENDLOOP.
only after your LOOP ... ENDLOOP operation is done try to delete the contents based on MATNR.
SORT the Table first based on matnr in ASCending order.
‎2013 Nov 08 9:44 AM
Yovish Bissessur wrote:
Hello,
Did you try this?
Loop into ur itab(it_mseg) into wa_mseg.
if wa_msg-matnr ne pr_matnr.
DELETE it_mseg WHERE matnr ne pr_matnr.
endif.
endloop.
the delete even though it is inside the loop will delete all records matching the condition in first attempt. i mean if you write the same loop outside the loop also behaves in the same because it is deleting everything instead you can use index to delete.
@ manisha it is not a good practice to delete records in loop.
‎2013 Nov 08 10:48 AM
How all records will be deleted when this comparison is being made?
Loop into ur itab(it_mseg) into wa_mseg.
if wa_msg-matnr ne pr_matnr.
DELETE it_mseg WHERE matnr ne pr_matnr.
endif.
endloop.
Instead the final internal table will have all values having matnr equal to pr_matnr.
I guess this is the requirement of the person initially.
‎2013 Nov 08 10:54 AM
i meant, when the control reaches DELETE statement for the first time it will delete all the Records which satisfies the condition( where matnr NE pr_matnr). next time the control will never enter IF.
‎2013 Nov 08 12:16 PM
yes damn right.
Only once it will be done.
May be, the Exit command could be used to skip the loop
‎2013 Nov 08 6:47 AM
‎2013 Nov 08 6:49 AM
‎2013 Nov 08 6:53 AM
Hi,
Why are you fetching unwanted data and then go for deleting it later ?
Its better to filter the records in the SELECT query itself rite.
You can do something like:
SELECT <fields>
from MSEG
INTO TABLE it_mseg
where MATNR EQ pr_matnr.
Thanks,
Ajay Bose
‎2013 Nov 08 7:24 AM
‎2013 Nov 08 8:49 AM
Hi,
You can do that, but with little modification
Don't use DELETE statement in LOOP and ENDLOOP statement, instead set a flag if the condition is met.
Note:- If we use DELETEstatement in LOOP, every time the statement is executed, it has to regenerate the index, which will effect the performance if the records are more in the internal table.
Then with DELETE statement delete all the records with the flag is set outside the LOOP.
you can use this code
LOOP AT itab INTO wa_itab.
w_tabx = sy-tabix.
READ TABLE itab2 INTO wa_itab2 WITH KEY field = wa_itab-field.
IF sy-subrc = 0.
wa_itab-flag = 'X'.
MODIFY itab INDEX w_tabx FROM wa_itab TRANSPORTING flag.
ENDIF.
CLEAR w_tabx.
ENDLOOP.
DELETE itab WHERE flag = 'X'.
‎2013 Nov 08 8:59 AM
Hi,
With delete options u can many options like CA (contains any) CO (contains only) CP (contains patterns etc). U can use this facility to delete entries from internal table.
With regards
Suneesh