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

deleting data from internal table based on parameter selection

Former Member
0 Likes
4,542

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.

19 REPLIES 19
Read only

Former Member
0 Likes
2,578

Hi,

You can write it as below:

DELETE it_mseg WHERE matnr NE pr_matnr.

Thanks & Regards

Priyesh Shah

Read only

0 Likes
2,578

can i put this in loop

Read only

0 Likes
2,578

i write same but it shows all the data

Read only

0 Likes
2,578

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.

Read only

0 Likes
2,578

thnks..

Read only

Former Member
0 Likes
2,578

I think you can put the NE in the selection statement itself where you fetch this material...

Read only

0 Likes
2,578

This message was moderated.

Read only

Former Member
0 Likes
2,578

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.

Read only

0 Likes
2,578

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.

Read only

0 Likes
2,578

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.

Read only

0 Likes
2,578

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.

Read only

0 Likes
2,578

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.

Read only

0 Likes
2,578

yes damn right.

Only once it will be done.

May be, the Exit command could be used to skip the loop

Read only

A-J-S
Active Participant
0 Likes
2,578

Hi,

Can you post your code here ?

Thanks.

Read only

Former Member
0 Likes
2,578

Show me your code.

Read only

Former Member
0 Likes
2,578

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

Read only

0 Likes
2,578

this is only for test purpose...

Read only

Former Member
0 Likes
2,578

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'.

Read only

Former Member
0 Likes
2,578

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