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

Dynamic loop condtions

Former Member
1,334

Hi All,

i have 2 internal tables, 1 is coming from ECC and the other is coming from Flatfile upload with similar structure, i need to compare these two fields based on the Key if key field matches then i need to delete the corresponding record of 2nd internal table.

challenges i am facing are

1. these 2 internal tables are dynamic

2. i am creating a 3 rd internal table to fetch the Key fields of ECC table from DD03L table

3. i need to loop on the 1st internal table and delete the records of 2nd internal table.

please help how to proceed

1 ACCEPTED SOLUTION
Read only

sandeep_ramesh88
Explorer
0 Likes
998

The scenario is quite easy as you will get the solution by looking at any of the links.

But have a look at this also:

Suppose <GIT_SFLIGHT> and <GIT_SCARR> are the 2 internal tables(Field symbols) of type sflight and scarr respectively.

Data: gwa_sflight type sflight,

           gwa_scarr   type scarr.

Now you have to delete the entries from <GIT_SCARR>.

SORT <GIT_SFLIGHT> BY CARRID ASCENDING.

SORT <GIT_SCARR> BY CARRID ASCENDING.

LOOP AT <GIT_SFLIGHT> INTO GWA_SFLIGHT.

READ TABLE <GIT_SCARR> INTO GWA_SCARR WITH KEY CARRID = GWA_SFLIGHT-CARRID BINARY SEARCH.

IF SY-SUBRC  = 0.

DELETE <GIT_SCARR> INDEX SY-TABIX.


ENDIF.


ENDLOOP.

Hope this helps....


8 REPLIES 8
Read only

Former Member
0 Likes
998

Please have a look on this

Read only

0 Likes
998

Hello,

Use these links..

http://scn.sap.com/thread/1694646

http://wiki.sdn.sap.com/wiki/display/ABAP/Dynamic+Internal+table

now 2 point will be cleared and for 3rd one.

Loop 1st Itab and inside loop read 2nd itab and if sy-subrc = 0 delete the data from same 2nd  itab using same WA which is for 2nd itab.

BR

Chandra..

Read only

0 Likes
998

Please have a look at field symbols<>. If not, please go through scn and you would be able to solve this easily.

Read only

sandeep_ramesh88
Explorer
0 Likes
999

The scenario is quite easy as you will get the solution by looking at any of the links.

But have a look at this also:

Suppose <GIT_SFLIGHT> and <GIT_SCARR> are the 2 internal tables(Field symbols) of type sflight and scarr respectively.

Data: gwa_sflight type sflight,

           gwa_scarr   type scarr.

Now you have to delete the entries from <GIT_SCARR>.

SORT <GIT_SFLIGHT> BY CARRID ASCENDING.

SORT <GIT_SCARR> BY CARRID ASCENDING.

LOOP AT <GIT_SFLIGHT> INTO GWA_SFLIGHT.

READ TABLE <GIT_SCARR> INTO GWA_SCARR WITH KEY CARRID = GWA_SFLIGHT-CARRID BINARY SEARCH.

IF SY-SUBRC  = 0.

DELETE <GIT_SCARR> INDEX SY-TABIX.


ENDIF.


ENDLOOP.

Hope this helps....


Read only

0 Likes
998

here i need to compare and delete based on the keyfields. main problem is key fields i need to determine by table DD03L and delete based on keyfields in table2.

i tried using dynamic where condition in Loop but it is giving error and even Read statement didn't work

Read only

0 Likes
998

what is the error you are getting ?

Read only

Former Member
0 Likes
998

If you want to compare key-components of table lines you should be familiar with field-Symbols and Assign Component [Name]|[index] of structure [struc] to [comp].

To evaluate which Componts belong to the Primary key, you can use FunctionModule  DDIF_FIELDINFO_GET. It Returns a table of type dfies. Structure dfies has a component "Keyflag" which means the component is part of the Primary key.

So you can compare component a of tab 1 with component of tab 2 by same Position(column, if you like) or same Name...

It depends on how dynamic your line spec is...

In newer SAP-Releases you can build a where-clause dynamically and use LOOP AT itab ASSIGNING|INTO fs|var WHERE (dyn.where-clause).

I did it in 7.31 and deployed to 7.01, where the syntaxchecker said: "NO!"

Regards

Jan Martin

Read only

0 Likes
998

Hi,

Try the following code, main concept is to create table type/structure dynamically, and create a structure/field symbol with only key fields. Then assign both entry of table 1 and table 2 to the same structure(two field symbol, of course) and check whether they are the same.

REPORT  zdynamic_loop.

PARAMETERS: tab1 TYPE char30,

            tab2 TYPE char30.



FIELD-SYMBOLS: <lt_1>       TYPE ANY TABLE,

               <lt_2>       TYPE ANY TABLE,

               <ls_1>       TYPE any,

               <ls_2>       TYPE any,

               <ls_key1>    TYPE any,

               <ls_key2>    TYPE any.

DATA:   itab_type           TYPE REF TO cl_abap_tabledescr,

        struct_type         TYPE REF TO cl_abap_structdescr,

        elem_type           TYPE REF TO cl_abap_elemdescr,

        comp_tab1           TYPE cl_abap_structdescr=>component_table,

        comp_tab2           TYPE cl_abap_structdescr=>component_table,

        comp_tab3           TYPE cl_abap_structdescr=>component_table,

        comp_fld            TYPE cl_abap_structdescr=>component,

        dref                TYPE REF TO data,

        lt_keys             TYPE TABLE OF dd03l.



TRY.

  "create the first table dynamically

  struct_type ?= cl_abap_typedescr=>describe_by_name( tab1 ).

  itab_type   = cl_abap_tabledescr=>create( struct_type ).

  comp_tab1 = struct_type->get_components( ).

  CREATE DATA dref TYPE HANDLE itab_type.

  ASSIGN dref->* TO <lt_1>.

  CREATE DATA dref TYPE HANDLE struct_type.

  ASSIGN dref->* TO <ls_1>.

  SELECT * FROM (tab1) INTO CORRESPONDING FIELDS OF TABLE <lt_1>.



  "create the second table dynamically

  struct_type ?= cl_abap_typedescr=>describe_by_name( tab2 ).

  itab_type   = cl_abap_tabledescr=>create( struct_type ).

  comp_tab2 = struct_type->get_components( ).

  CREATE DATA dref TYPE HANDLE itab_type.

  ASSIGN dref->* TO <lt_2>.

  CREATE DATA dref TYPE HANDLE struct_type.

  ASSIGN dref->* TO <ls_2>.

  SELECT * FROM (tab2) INTO CORRESPONDING FIELDS OF TABLE <lt_2>.



  "create a structure with key fields only

  SELECT * FROM dd03l INTO TABLE lt_keys WHERE tabname = tab1 AND keyflag = 'X'.

  LOOP AT comp_tab1 INTO comp_fld.

    READ TABLE lt_keys TRANSPORTING NO FIELDS WITH KEY fieldname = comp_fld-name.

    IF sy-subrc = 0.

      APPEND comp_fld TO comp_tab3.

    ENDIF.

  ENDLOOP.

  struct_type ?= cl_abap_structdescr=>create( comp_tab3 ).

  CREATE DATA dref TYPE HANDLE struct_type.

  ASSIGN dref->* TO <ls_key1>.

  CREATE DATA dref TYPE HANDLE struct_type.

  ASSIGN dref->* TO <ls_key2>.



  "delete from the second table for same keys in the first table

  LOOP AT <lt_1> INTO <ls_1>.

    MOVE-CORRESPONDING <ls_1> TO <ls_key1>.

    LOOP AT <lt_2> INTO <ls_2>.

      MOVE-CORRESPONDING <ls_2> TO <ls_key2>.

      IF <ls_key1> = <ls_key2>.

        DELETE TABLE <lt_2> from <ls_2>.

      ENDIF.

    ENDLOOP.

  ENDLOOP.



ENDTRY.