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: 

Hello experts..deleting entries in internal table.

Former Member
0 Kudos

I am having internal table with fields ...mblnr werks bwart matnr menge etc. The records are like this

mblnr werks bwart matnr menge etc

1) 2222200123 an20 10 12111111111 20 .....

2) 2222200123 an20 98 12111111119 20 .....

3) 2222200123 an20 99 12111111113 20 .....

4) 2222200124 an20 10 12111111112 20 .....

5) 2222200125 an20 25 12111111115 20 .....

6) 2222200125 an20 98 12111111117 20 .....

7) 2222200125 an20 97 12111111121 20 .....

I want to have the records that have same document number.ie in above i want 1, 2,3, 5,and 6, 7 but not 4 because that is only single document. ie I wanted the repeated document number records .so please tell me what to do .Thank you very much.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Shiva,

Srihari's code looks very good, although it could be optimized a little.

The others have problems.

Sudhir's code doesn't compare the correct field (it uses matnr, when it should use mblnr).

In addition, the form of the DELETE that he uses is incorrect.

He uses

DELETE FROM itab USING lv_index.

which should be

DELETE itab INDEX lv_index.

Even if these were corrected, it is extremely inefficient, with a loop inside a loop.

-


John's code is inefficient since the DELETE will cause a sequential access each time, since he is not deleting using the index.

In addition, I am uncertain about the procedure he is using in moving itab to itab_temp. If itab_temp has unique keys, it will cause a short dump.

If it doesn't, there will be no short dump, but I do not know what is to be gained by it.

It looks like you wouldn't delete what you expect with his code.

The read should get a hit everytime, so what will be deleted?

-


Srihara's code could be optimized as follows, though the changes I suggest are trivial. Srihara deserves your thanks most of all:

FIELD-SYMBOLS: <wa_mat> type any.
DATA: count type i.
 
SORT it_tab BY mblnr.
 
LOOP AT it_tab ASSIGNING <wa_mat>.
  count = count + 1.
 
  AT END OF mblnr.
    IF count = 1.
      CLEAR <wa_mat>-mblnr.
    ENDIF.
    CLEAR count.
  ENDAT.
 
ENDLOOP.
 
DELETE it_tab WHERE mblnr IS INITIAL.

-


Good luck

Nice code Srihara. I'll remember that.

Brian

Minor edits, clarify explanation.

Message was edited by:

Brian Sammond

4 REPLIES 4

Former Member
0 Kudos

User SORT on the internal table with the Document Number.

itab_tmp[] = itab[].

LOOP AT itab.

lv_index = sy-tabix.

CLEAR count.

LOOP AT itab_tmp WHERE matnr = itab_tmp-matnr.

count = count + 1.

ENDLOOP.

IF count EQ 1.

DELETE FROM itab USING lv_index.

ENDIF.

ENDLOOP.

Message was edited by:

Sudhir K Atluru

Former Member
0 Kudos

Hello,

Try this:

itab type table of <your_type>,

itab_temp type sorted table of <your_type>.

data: wtab like line of itab.

Itab_temp[] = itab[].

Loop at itab into wtab.

read table itab_temp with key [document] = wtab-[document]

transporting no fields.

if sy-subrc <> 0.

delete table itab from wtab.

endif.

Endloop.

Hope it helps,

Regards,

John.

Former Member
0 Kudos

This is how I would do it :

FIELD-SYMBOLS: <wa_mat> type any.
DATA: count type i,
      blank type mblnr.

SORT it_tab BY mblnr.

LOOP AT it_tab ASSIGNING <wa_mat>.
count = count + 1.

AT END OF mblnr.
if count = 1.
clear <wa_mat>.
endif.
clear count.
ENDAT.

ENDLOOP.

DELETE it_tab WHERE mblnr = blank.

Message was edited by:

Srihari Hebbar

Former Member
0 Kudos

Shiva,

Srihari's code looks very good, although it could be optimized a little.

The others have problems.

Sudhir's code doesn't compare the correct field (it uses matnr, when it should use mblnr).

In addition, the form of the DELETE that he uses is incorrect.

He uses

DELETE FROM itab USING lv_index.

which should be

DELETE itab INDEX lv_index.

Even if these were corrected, it is extremely inefficient, with a loop inside a loop.

-


John's code is inefficient since the DELETE will cause a sequential access each time, since he is not deleting using the index.

In addition, I am uncertain about the procedure he is using in moving itab to itab_temp. If itab_temp has unique keys, it will cause a short dump.

If it doesn't, there will be no short dump, but I do not know what is to be gained by it.

It looks like you wouldn't delete what you expect with his code.

The read should get a hit everytime, so what will be deleted?

-


Srihara's code could be optimized as follows, though the changes I suggest are trivial. Srihara deserves your thanks most of all:

FIELD-SYMBOLS: <wa_mat> type any.
DATA: count type i.
 
SORT it_tab BY mblnr.
 
LOOP AT it_tab ASSIGNING <wa_mat>.
  count = count + 1.
 
  AT END OF mblnr.
    IF count = 1.
      CLEAR <wa_mat>-mblnr.
    ENDIF.
    CLEAR count.
  ENDAT.
 
ENDLOOP.
 
DELETE it_tab WHERE mblnr IS INITIAL.

-


Good luck

Nice code Srihara. I'll remember that.

Brian

Minor edits, clarify explanation.

Message was edited by:

Brian Sammond