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: 

delete itab

Former Member
0 Kudos
257

Hi experts,

I have ITAB like this.


QMNUM        MNCOD       TEXT

000200000163|MS      |AAAAAAAAAAAA|
000200000163|MS      |BBBBBBBBBBBB|
000200000163|MS      |            |
000200000210|EN      |EEN200000210|
000200000210|EN      |            |
000200000220|EN      |EEN200000210|
000200000220|EN      |EEN200000211|
000200000220|EN      |EEN200000211|
000200000220|EN      |EEN200000211|

In the TEXT column any one of the rows are empty, i want to delete entire particular QMNUM.

For ex:QMNUM is having 3 entries of 000200000163.<b>Since 3rd row is empty, i want to delete all 000200000163 rows.</b>

so result will be like this


000200000210|EN      |EEN200000210|
000200000210|EN      |            |
000200000220|EN      |EEN200000210|
000200000220|EN      |EEN200000211|
000200000220|EN      |EEN200000211|
000200000220|EN      |EEN200000211|

How can i do that?

Reward guaranteed

kaki<b></b>

1 ACCEPTED SOLUTION

former_member188685
Active Contributor
0 Kudos
233

Hi Kaki,

i am taking a temp table of type itab,

itab_tmp[] = itab[]
sort itab_tmp by qmnum.
delete adjacent duplicates from itab_tmp comparing qmnum .
data: l_tabix type sy-tabix.
loop at itab.
read table itab_tmp with key qmnum = itab-qmnum
                                binary search.
if sy-subrc = 0 and itab-text is initial.
delete itab where qmnum = itab_tmp-qmnum.
endif.

endloop.

now itab will have the contents what ever you required..

check this logic...

regards

vijay

20 REPLIES 20

Former Member
0 Kudos
233

sort itab by QMNUM.

loop at itab.

delete itab where itab-text = space.

endloop.

former_member186741
Active Contributor
0 Kudos
233

loop at itab.

check itab-text is initial.

delete itab where qmnum = itab-qmnum.

endloop.

Former Member
0 Kudos
233

data:begin of it_new occurs 0,

qmnum like ztab-qmnum,

end of it_new.

sort itab by qmnum.

loop at itab.

if itab-text is initial.

it_new-qmnum = itab-qmnum.

append it_new.

clear it_new.

endif.

endloop.

loop at it_new.

read table itab with key qmnum = it_new-qmnum.

if sy-subrc = 0.

delete itab.

endif.

endloop.

former_member186741
Active Contributor
0 Kudos
233

row 5 has empty text too - why don't you want to delete

that one?

If you do want to delete it, use my earlier solution.

If you don't want to delete it - explain the spec in more detail.

Message was edited by: Neil Woodruff

Former Member
0 Kudos
233

Set up a range table for QMNUM.

Loop at the itab. If the text is initial, add QMNUM to the range table (Include, EQ). When the loop is complete, delete itab where QMNUM is in the range table.

Rob

0 Kudos
233

ranges r_qanum.

loop at itab.

r_qanum-low = itab-qanum.

r_qanum-sign = 'I'.

r_qanum-option = 'EQ'.

append r_qanum.

endloop.

loop at itab.

read table itab where text = space.

if sy-subrc = 0.

delete itab where qanum in r_qanum.

endif.

endloop

former_member188685
Active Contributor
0 Kudos
234

Hi Kaki,

i am taking a temp table of type itab,

itab_tmp[] = itab[]
sort itab_tmp by qmnum.
delete adjacent duplicates from itab_tmp comparing qmnum .
data: l_tabix type sy-tabix.
loop at itab.
read table itab_tmp with key qmnum = itab-qmnum
                                binary search.
if sy-subrc = 0 and itab-text is initial.
delete itab where qmnum = itab_tmp-qmnum.
endif.

endloop.

now itab will have the contents what ever you required..

check this logic...

regards

vijay

0 Kudos
233

Hi Vijay Babu Dudla,

Thanks a lot.

Full points alloted....

And also can you pls tell me what is the use of "Binary search" in read table?

read table itab_tmp with

key qmnum = itab-qmnum binary search.

kaki

0 Kudos
233

Binary search is used to search efficiently by giving a key field.

It can reduce the number of search steps... and thus improves the search..

But to do that keep the table in sorted form.

0 Kudos
233

Thank you Wenceslaus G.

Got it.

0 Kudos
233

Hi,

sorry for late reply...

i was busy with some work..

Binary search will improve performance, and you must sort the table with the key(s) what ever you are going to use in read statement or else you will get the wrong results(some times). sort before using binary search is must.

regards

vijay

Former Member
0 Kudos
233

Hi Kaki R,

A simpler solution would be to actually remove the loop also. You just need to do as below.

<b>delete itab where text = space.</b>

Hope this helps.

Pls reward points and close this thread, if your problem is solved.

Rgds,

Prabhu.

Former Member
0 Kudos
233

Your example is not correct. If you want to delete all QMNUM rows even if one QMNUM row is blank, the your final internal table should look like this.

000200000220|EN      |EEN200000210|
000200000220|EN      |EEN200000211|
000200000220|EN      |EEN200000211|
000200000220|EN      |EEN200000211|

because even QMNUM 000200000210 has one row with a blank TEXT value.

Try this code.

[code]

itab_temp[] = itab[].

loop at itab_temp where text = space.

delete itab where qmnum = itab_temp-qmnum.

endloop.

Former Member
0 Kudos
233

Hi Kaki,

Just an advice , dont use the code given by Vijay Babu Dudla , as it will be poor in performance.

We dont know how much data your internal table contains but if its a huge table, that code will unncessarily sort and serach the table and remove duplicates which is not stated in ur problem statment. This code is very expensive on resources belive me .....

I will rather recommend u to use the techniqe specified by Prashant Prabhu .... its the best and most commonly used solution to ur problem... I guess Prashant Prabhu deserves full points too : ).

And if u being generous u can give some to me as well : )

regards,

Sumeet Mishra

0 Kudos
233

Hi Sumeet,

Pls read my problem carefully.

I dont want only deleting itab if text eq ''.In the itab any texts are null i want to delete all qmnum with related to text.What prashant is saying is, it deletes only texts are null.Remaining qmnums will be there.

Regards

kaki

0 Kudos
233

Hi Sumeet,

I actually mis-understood the question. My soln will not work. <i>Sorry guys !!</i>

Prash.

Former Member
0 Kudos
233

Try this Kaki,

<i>sort itab by text ascending.

loop at itab where text = space.
delete itab where qmnum = itab-qmnum.
endloop.</i>

I would think this is performance wise better.

the idea is to get the table sorted with all the qmnum with text blank at the top of the itab.

And then loop at itab for all the qmnum with blank texts. and delete all records of this particular qmnum.

Pls try this and let me knwo. Sorry for the earlier post.

Rgds,

Prabhu.

Former Member
0 Kudos
233

hi,

here is a bit shuffle of the logic

ranges r_qmnum.

loop at itab.

If itab-text = space.

r_qanum-low = itab-qmnum.

r_qanum-sign = 'I'.

r_qanum-option = 'EQ'.

append r_qmnum.

endif.

endloop.

loop at itab.

delete itab where qmnum in r_qmnum.

endloop.

i suppose this will solve ur scenario.the logic is simple catch all those QMNUMS whose text is empty & then loop the itab & the rows where the QMNUM matches with the ranges.

Former Member
0 Kudos
233

Hi Kaki,

I am sorry I misinterpreted your problem...

But let me suggest u a better way of doing this in case u have performance issue with the same.

use a range table to remove the unwanted records from ur internal table. I guess Rob has suggested the same.

Now why I am stressing on this is becasue the process SORT and DELETE DUPLICATE are pretty expensive process in itself and are used only if must.

So incase ur internal table contains a huge data u might well end up using a lot of hard ware resources for creating a temprory table and then sorting and then removing duplicates from it. Instead creating a range table and then using DELETE ITAB FROM range table will imporove the performance.

I hope this help u out in improving your code : )

regards,

Sumeet Mishra

Former Member
0 Kudos
233

Kaki - a lot of the suggestions you got on this thread involve something like:


loop at itab.
  delete itab where...
endloop.

This can be dangerous. If you look at the help for DELETE, you'll see that "If you delete lines within a LOOP ... ENDLOOP block, the deletion affects subsequent loop passes."

Assuming I understand your requirements, I think that the safest approaches are the ones described by Srinivas and me. The worked out code for mine is:

RANGES r_q FOR itab-qmnum.

r_q-option = 'EQ'.
r_q-sign   = 'I'.

LOOP AT itab WHERE text IS initial.
  r_q-low = itab-qmnum.
  APPEND r_q.
ENDLOOP.

SORT r_q.
DELETE ADJACENT DUPLICATES FROM r_q.

DELETE itab WHERE qmnum IN r_q.

Rob