Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 8411

Re: Dynamic loop condtions

$
0
0

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.

https://webowt.dmzwdf.sap.corp:443/sap/bc/bsp/spn/bor01/main.do


Viewing all articles
Browse latest Browse all 8411

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>