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 rows dynmically from internal table

0 Kudos

Hi Gurus,

I have got an problem. I have an internal table itab.

Case 1. It has got some 10 rows. I want to delete rows between those two rows containing same field value of a Field 'X'. Here same value of that field could come on 1st and 7th row. Then mean, I need to delete 2nd to 6th row.

It could come in 4th and 10th row. Then I need to delete 5th to 9th row.

Case 2. It has got some 20 rows. Now same I want to delete rows between those two rows containing same field value of a Field 'X'. Here again, same value of that field could come on 1st and 17th row. Mean I will delete 2nd to 16th row. It could come in 7th and 19th row. Then I need to delete 8th to 18th row.

13 REPLIES 13

Former Member
0 Kudos

Hi,

loop itab into fs.

if fs-field EQ 'X'.

delete itab from fs.

endif.

clear fs.

endloop.

0 Kudos

Hi Sree,

Let me give more light in the issue. Let see below that field values in itab having 10 rows

a

b

c

g

d

r

c

e

c

d

See 3rd row and 7th. Both contaiing same value (c). Now, I want to delete 4th(g), 5th(d) and 6th(r). But row 9 also containing value c. I dont want to delete values after this 9th row c.

Your code will delete this C also. I want to delete rows only once not after it has deleted the rows.

I know its a tricky but that is my requirement.

Regards

0 Kudos

>

> Hi Sree,

> Let me give more light in the issue. Let see below that field values in itab having 10 rows

>

> a

> b

> c

> g

> d

> r

> c

> e

> c

> d

> See 3rd row and 7th. Both contaiing same value (c). Now, I want to delete 4th(g), 5th(d) and 6th(r). But row 9 also containing value c. I dont want to delete values after this 9th row c.

> Your code will delete this C also. I want to delete rows only once not after it has deleted the rows.

> I know its a tricky but that is my requirement.

>

> Regards

Hi Vaibhav,

Does your table have only 10 records? Is it fixed?

What if we have 'a' as repeition ?

Can we assume after the 3rd occurence of a value we donot delete anything?

Your req. is not tricky, it is rather weird.

Suhas

0 Kudos

Hi Suhas,

No table can any number of values and repetion of 'a' can be of many. Not necessary of 3 or 4....

For some extent I am agree that its a weird, but if it wasnt no way I woud have posted. Kindly help me out.

Regards

Edited by: Vaibhav bhardwaj on Oct 30, 2009 11:07 AM

0 Kudos

Hello,

Here I have a doubt about the expected functionality:

case I :

a

b

c

g

d

r

c

e

c

d

- In this case what are the rows you are expecting to be deleted?

Case II:

a

b

c

g

d

r

c

e

c

d

a

e

f

c

a

- In this case what are the rows you are expecting to be deleted?

Can you calrify once?

Regards,

Swarna Munukoti

Former Member
0 Kudos

Hi,

do a loop over your table with where clause

'where field = 'X'.'

sign value to 2 variables (variable = sy-index)

and exit the loop when second one is filled.

variable1 = variable1 + 1.

variable2 = variable2 - 1.

delete table from variable1 to variable2.

grtz,

Koen

0 Kudos

Hi Labie,

You are very close. But see my reply to Sree. In your solution, when that loop fire again, will delete the 9th row value. Which I dont want. Pz help me.

Regards

Vaibhav

Former Member
0 Kudos

take the data in the int tab to another temp int tab.

sort the temp int tab by the field 'X'.

declare 3 variables w_row1, w_row2, w_temp.

LOOP at temp_int_tab.

CLEAR : w_row1, w_row2.

LOOP AT int_tab where value of field 'X' = temp_int_tab-value of field 'X'.

w_row1 = sy-tabix.

IF w_row1 is not initial.

w_row2 = sy-tabix.

ENDIF.

ENDLOOP.

ENDLOOP.

DELETE int_tab FROM w_row1 TO w_row2.

Regards,

Srinivas

0 Kudos

Hi Srinivas,

In that case table index value will be same. Also, how I can then I klnow the next row containing the same value of the field where actually my challenge lies.

Regards

0 Kudos

vaibhav,

the same value will not be there in both varaibles.

For the first round of loop, the value of sy-tabix is stored in w_row1. The second time the same value is come across, since there is a value in w_row1, w_row2 will be filled.

After the loop. Add this part of the code too....

w_row1 = w_row1 + 1.
w_row2 = w_row2 - 1.

Therefore both are populated.

Adv of this is that even if the repetition is n times the rows between the 1st and nth are deleted(excluding 1st and nth).

Regards,

Srinivas

praveen_hannu
Contributor
0 Kudos

Hi Vaibhav

Please find the code, I hope it helps

 DATA: BEGIN OF itab OCCURS 10,
      val,
      END OF itab.

DATA: jtab LIKE itab OCCURS 0 WITH HEADER LINE,
      l_ind1 TYPE i,
      l_ind2 TYPE i,
      l_lines TYPE i,
      l_flg.

itab-val = 'a'.
append itab.

itab-val = 'b'.
append itab.

itab-val = 'c'.
append itab.

itab-val = 'd'.
append itab.

itab-val = 'e'.
append itab.

itab-val = 'f'.
append itab.

itab-val = 'c'.
append itab.

itab-val = 'g'.
append itab.

itab-val = 'c'.
append itab.

itab-val = 'h'.
append itab.

jtab[] = itab[].

DESCRIBE TABLE itab[] LINES l_lines.

LOOP AT itab.

MOVE sy-tabix to l_ind1.

l_ind2  = l_ind1 + 1.

LOOP AT jtab FROM l_ind2 to l_lines WHERE val eq itab-val.
  l_ind1 = l_ind2.
  l_ind2 = sy-tabix.
  l_flg = 'X'.
  exit.
ENDLOOP.

IF l_flg eq 'X'.
  exit.

ENDIF.
ENDLOOP.

delete itab FROM l_ind1 to l_ind2.

LOOP AT itab.
  WRITE: /2 itab-val.
ENDLOOP. 

Still if you want to apply this multiple values, create another internal table and store the values which have been deleted already. So before you going to delete them again you can check from these new internal table.

Please let me know, if any issues still exists.

Regards

Praveen

0 Kudos

Hey Praveen,

Thanks for your solution. You are savour.

Hey Srinivas,

Your solution is right, but my requirement is to know the 'UPTO' row in which rows to be deleted. I am able to find the starting row and it is also taking care but till which row, where things are getting out of my hand.

Thanks a lot to all of you.

Regards

0 Kudos

Thanks