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: 

Internal tables

Former Member
0 Kudos

hai all

i have two internal tables namely itab1 itab2

itab1 contains 3 records

itab2 contains 1 lakh records

whatever the 3 records in the itab1 will be in the itab2...

my requirement is i want to delete all the records other than that 3 records. in itab2.....

how to do this..?

Regards

Kiran

Edited by: kiran kumar on Sep 1, 2008 7:10 AM

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Kiran,

As you want to delete data of one table based on records in another table.

For this you can use this type of logic,


  LOOP AT i_tab1 INTO fs_tab1.
    w_tabix = sy-tabix.
    IF i_tab2 IS INITIAL.
      DELETE i_tab1.
    ELSE.
      READ TABLE i_tab2 INTO fs_tab2 WITH
                                  KEY vbeln = fs_tab1-vbeln.
      IF sy-subrc NE 0.
        DELETE i_tab1 INDEX w_tabix.
      ENDIF.                         
    ENDIF.                          
  ENDLOOP.        

Regards

Abhijeet

6 REPLIES 6

Former Member
0 Kudos

Hi Kiran,

As you want to delete data of one table based on records in another table.

For this you can use this type of logic,


  LOOP AT i_tab1 INTO fs_tab1.
    w_tabix = sy-tabix.
    IF i_tab2 IS INITIAL.
      DELETE i_tab1.
    ELSE.
      READ TABLE i_tab2 INTO fs_tab2 WITH
                                  KEY vbeln = fs_tab1-vbeln.
      IF sy-subrc NE 0.
        DELETE i_tab1 INDEX w_tabix.
      ENDIF.                         
    ENDIF.                          
  ENDLOOP.        

Regards

Abhijeet

Former Member
0 Kudos

Hi Kiran,

Try the following:

LOOP AT itab2.
    w_index = sy-tabix.
   
      READ TABLE itab1  WITH KEY matnr = itab2-matnr.
     
      IF sy-subrc NE 0.
        DELETE itab2 INDEX w_index.
      ENDIF.                         
    ENDIF.    
                      
  ENDLOOP.

Regards,

Chandra Sekhar

Former Member
0 Kudos

LOOP AT i_tab1 INTO wa_tab1.

READ TABLE i_tab2 INTO wa_tab2 WITH

KEY field1 = wa_tab1-field1

field2 = wa_tab1-field2

field3 = wa_tab1-field3.

IF sy-subrc NE 0.

DELETE i_tab1 from wa_tab2.

ENDIF.

ENDIF.

ENDLOOP.

Edited by: murali papana on Sep 1, 2008 1:33 AM

former_member585060
Active Contributor
0 Kudos

Hi,

Tyr like the following code in ur program,

=========================================

TABLES : marc.

TYPES : BEGIN OF ty_marc,

matnr TYPE mara-matnr,

END OF ty_marc.

SELECT-OPTIONS : s_werks FOR marc-werks.

DATA : it_mara TYPE TABLE OF ty_marc,

it_marc TYPE TABLE OF ty_marc,

wa_mara TYPE ty_marc,

wa_marc TYPE ty_marc.

DATA : wa_index TYPE sy-tabix.

SELECT matnr FROM mara INTO TABLE it_mara.

SELECT matnr FROM marc INTO TABLE it_marc WHERE

werks IN s_werks.

BREAK-POINT.

LOOP AT it_mara INTO wa_mara.

wa_index = sy-tabix.

READ TABLE it_marc INTO wa_marc WITH KEY matnr = wa_mara-matnr.

IF sy-subrc NE 0.

DELETE it_mara INDEX wa_index.

MODIFY TABLE it_mara FROM wa_mara.

ENDIF.

ENDLOOP.

=========================================

Regards

Bala Krishna

Subhankar
Active Contributor
0 Kudos

Hi..

If the structure of the two table is same just do this..

REFRESH : I_TAB2.

I_TAB2[] = I_TAB1[].

If the structure are not same and you want to delete by comparing the key fields ...

define two internal table(I_TEMP1 and I_TEM2) type I_TAB2..

loop at i_tab1 into wa_itab.

REFRESH I_TEM1[].

i_temp1[] = I_TAB2.

DELETE FROM I_temp1 WHERE keys <> wa_ITAB1-keys.

APPEND LINES OF I_temp1 [ ] to I_tem2 [ ]

endloop.

REFRESH I_TAB2.

I_TAB2 [ ] = I_TEM2 [ ].

Edited by: Subhankar Garani on Sep 1, 2008 11:49 AM

former_member217544
Active Contributor
0 Kudos

Hi,

instead of deleting all the records other than those 3 records, i think its better if you use another internal table and read those 3 records into that internal table and use this 3rd internal table for further processing.

For example:


data: itab1 type starndard table of table1,
        itab2 type starndard table of table2,
        itab3 type starndard table of table2,
        wa_itab1 type table1,
        wa_itab2 type table2.

loop at itab1 into wa_itab1.

read table itab2 into wa_itab2 with key field1 = wa_itab1-field1.
if sy-subrc eq 0.
append wa_itab2 to itab3.
endif.
clear : wa_itab2,wa_itab1.

endloop.

Now you can do further processign using itab3 instead of using itab2.

Hope thsi will help.

Reagrds,

Swarna Munukoti.