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

Assign dynamic table to unknown table name

Former Member
0 Likes
4,004

This code creates a dynamic table for each table name in the loop, since there are so many tables and i won't know the table names i want to pass it ti <f1> etc and call it later on.

DATA : ref_tab       TYPE tabname,

             tbc_count  TYPE string VALUE 1.

FIELD-SYMBOLS :

                 <f1>    TYPE any table,

                 <f2>    TYPE any table,

                 <f3>    TYPE any table,

                 <f4>    TYPE any table,

                 <f5>    TYPE any table,

                  <dynamic>   TYPE any table.

LOOP AT checktab.

     CREATE DATA dataref TYPE TABLE OF (checktab-tabname).

     ASSIGN dataref->* TO <dynamic>.

     CONCATENATE '<f' tbc_count '>' INTO ref_tab.

     ASSIGN <dynamic> TO <ref_tab).

     tbc_count = tbc_count + 1.

ENDLOOP.

But i can't assign the dynamic table to ref_tab which contains the field symbol table name.

How to do it ?

This code creates a dynamic table for each table name in the loop, since there are so many tables and i won't know the table names i want to pass it ti <f1> etc and call it later on.

DATA : ref_tab       TYPE tabname,

             tbc_count  TYPE string VALUE 1.

FIELD-SYMBOLS :

                 <f1>    TYPE any table,

                 <f2>    TYPE any table,

                 <f3>    TYPE any table,

                 <f4>    TYPE any table,

                 <f5>    TYPE any table,

                  <dynamic>   TYPE any table.

LOOP AT checktab.

     CREATE DATA dataref TYPE TABLE OF (checktab-tabname).

     ASSIGN dataref->* TO <dynamic>.

     CONCATENATE '<f' tbc_count '>' INTO ref_tab.

     ASSIGN <dynamic> TO <ref_tab).

     tbc_count = tbc_count + 1.

ENDLOOP.

But i can't assign the dynamic table to ref_tab which contains the field symbol table name.

How to do it ?

2 REPLIES 2
Read only

Abhijit74
Active Contributor
0 Likes
1,251

Hello,

I just do some modification. Hope it will works. Please do the type reference correctly .

TYPES: BEGIN OF ty_checktab,

         srl_no   TYPE char1,

         tab_name TYPE tabname,

        END OF ty_checktab.

 

DATA : lv_tabname    TYPE tabname,

        lt_checktab   TYPE STANDARD TABLE OF ty_checktab,

        tbc_count     TYPE string VALUE 1,

        ls_chktab     TYPE ty_checktab,

        dataref       TYPE REF TO data.

FIELD-SYMBOLS :

                  <f1>         TYPE ANY TABLE,

                  <f2>         TYPE ANY TABLE,

                  <f3>         TYPE ANY TABLE,

                  <f4>         TYPE ANY TABLE,

                  <f5>         TYPE ANY TABLE,

                  <ref_tab>    TYPE any,

                  <dynamic>    TYPE ANY TABLE.

CLEAR lv_tabname.

LOOP AT lt_checktab INTO ls_chktab.

   CREATE DATA dataref TYPE (ls_chktab-tab_name).

   ASSIGN dataref->* TO <dynamic>.

   CONCATENATE '<f' tbc_count '>' INTO lv_tabname..

   ASSIGN lv_tabname TO <ref_tab>.

   ASSIGN <dynamic> TO <ref_tab>.

   tbc_count = tbc_count + 1.

ENDLOOP.

Please check below code . It is executing correctly.

PARAMETERS:
  TAB_NAME    LIKE SY-TNAME,               "Table name
  TAB_COMP    LIKE X031L-FIELDNAME,   "Field name
  ANZAHL      TYPE I DEFAULT 10.           Number of lines

FIELD-SYMBOLS: <WA>   TYPE ANY,
               <COMP> TYPE ANY.

  DATA: WA_REF TYPE REF TO DATA.

  CREATE DATA WA_REF TYPE (TAB_NAME).      "Suitable work area
  ASSIGN WA_REF->* TO <WA>.

SELECT * FROM (TAB_NAME) INTO <WA>
  UP TO anzahl ROWS.
  WRITE: / TAB_COMP, <COMP>.
  ENDSELECT.

Thanks,

Abhijit

Read only

PeterJonker
Active Contributor
0 Likes
1,251

This will work, but only if the table name that is constructed dynamically exists in the dictionary.

I used HR tables for this example

DATA: dyn_name TYPE tabname,
           part_name TYPE tabname,
           counter TYPE numc2.

  part_name = 'HRP10'.
  CONCATENATE part_name counter INTO dyn_name.

  PERFORM dosomething CHANGING dyn_name
                                                               part_name
                                                               counter.

FORM dosomething  CHANGING p_dyn_name TYPE tabname
                                                       p_part_name TYPE tabname
                                                       p_counter TYPE numc2.

  FIELD-SYMBOLS: <wa>   TYPE ANY,
                                <table> TYPE ANY TABLE.

  DATA: wa_ref TYPE REF TO data,
              tab_ref TYPE REF TO data.

      p_counter = p_counter + 1.

WHILE p_counter < 10.

     CREATE DATA wa_ref TYPE (p_dyn_name).

     CREATE DATA tab_ref TYPE TABLE OF (p_dyn_name).
     ASSIGN wa_ref->* TO <wa>.
     ASSIGN tab_ref->* TO <table>.

    SELECT * FROM (p_dyn_name) INTO TABLE <table>.

    LOOP AT <table> ASSIGNING <wa>.

    ENDLOOP.

    CONCATENATE p_part_name p_counter INTO p_dyn_name.

    PERFORM dosomething CHANGING p_dyn_name
                                                                 p_part_name
                                                                 p_counter.

ENDWHILE.

ENDFORM.                    " DOSOMETHING