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 table name to subroutine as parameter

preetamr
Explorer
0 Likes
6,308

Hi All,

While developing a program, I felt need of recursion in my program. So I was thinking of writing a perform that will call itself with a parameter that will keep track of it from becoming a never-ending loop. e.g.

data : n type i value '04',

          string_tab type string.

concatenate 'gt_tab' '01' into string_tab.

perform subroutine_name tables string_tab.

form subroutine_name tables p_table.

     if n gt '01'.

     "My logic

     n = n - 1.

     concatenate 'gt_tab' n into string_tab.

     perform subroutine_name tables string_tab.

     else.

     exit.

     endif.

endform.

However, I am unable to give a name of table to perform. My question is can we do that somehow as we did in select?

e.g.

data v_stringtab type string value 'MARA'.

select *

from (v_stringtab)

into table gt_mara.

Is it possible in SAP ABAP? Any Answer or hint is fine. I need some hint to proceed to right direction.

Hi All,

While developing a program, I felt need of recursion in my program. So I was thinking of writing a perform that will call itself with a parameter that will keep track of it from becoming a never-ending loop. e.g.

data : n type i value '04',

          string_tab type string.

concatenate 'gt_tab' '01' into string_tab.

perform subroutine_name tables string_tab.

form subroutine_name tables p_table.

     if n gt '01'.

     "My logic

     n = n - 1.

     concatenate 'gt_tab' n into string_tab.

     perform subroutine_name tables string_tab.

     else.

     exit.

     endif.

endform.

However, I am unable to give a name of table to perform. My question is can we do that somehow as we did in select?

e.g.

data v_stringtab type string value 'MARA'.

select *

from (v_stringtab)

into table gt_mara.

Is it possible in SAP ABAP? Any Answer or hint is fine. I need some hint to proceed to right direction.

18 REPLIES 18
Read only

Former Member
0 Likes
3,594

Dear, Declare your itab globally.

TYPES: itab1 TYPE STANDARD TABLE <TABLE_name>

PERFORM FORM_FILL CHANGING itab1.

FORM FORM_FILL CHANGING itab1 type <TABLE_name>.

****************Your Logic

ENDFORM.

Search on Google for the same. or look into the thread.. "" passing internal table into a subroutine..""

Read only

Former Member
0 Likes
3,594

Hi Preetam,

You can use TABLE


Perform select_details using lx_final lv_colu changing lt_pernr.


Form select_details  using    px_final    type zst_manpower

                                         pv_colu    type  i

                           changing pt_pernr   type table.

Endform

SuDEESH

Read only

Former Member
0 Likes
3,594

Hi,

First Declare the table as TYPE STANDARD TABLE.

Then call the Form,

PERFORM saubroutine_name TABLES itab.

form subroutine_name tables p_table like itab[].

     <logic>.

endform.

Read only

preetamr
Explorer
0 Likes
3,594

@All: I think I was not clear enough for all of you.
Let's say my table name keeps changing, then what.

I want same perform to recall itself with different table name as per the situation.

e.g.

perform subroutine_name tables itab1.

form subroutine_name tables p_table.

     perform subroutine_name tables itab2.

endform.

itab1 and itab2 both have same structure. however, i want to use a variable that can pass the name of table to the perform. Is that possible?

Read only

Former Member
0 Likes
3,594

If it is dynamic.. Just mention TYPE TABLE.

Eg..

form select_details  using    px_final    type zst_manpower

                                                   pv_colu    TYPE I

                     changing pt_pernr    type table.

This will work! But I'm using the same.

SuDEESH

Read only

0 Likes
3,594

@Sudeesh: Type of table is not dynamic. Name of table is dynamic. like... itab1, itab2, itab3.

All of three tables are of same structure.

Read only

Former Member
0 Likes
3,594

No, i don't think so. because  in

perform subroutine_name tables itab1.

form subroutine_name tables p_table.

You are using Tables means it will look for a table(internal table) not for a variable..

Read only

0 Likes
3,594

@Chandra: Can I use using or changing for internal tables and pass names to perform?

Read only

Former Member
0 Likes
3,594

Try this code..

DATA : BEGIN OF LINE OCCURS 0,

       LINE1 TYPE I,

       LINE2 TYPE I,

       LINE3 TYPE I,

       END OF LINE.

DATA : INT_NAME(10) TYPE C,

       FUN_NAME(10) TYPE C,

       SORT1(10) TYPE C,

       SORT2(10) TYPE C.

LINE-LINE1 =  1.

LINE-LINE2 =  10.

LINE-LINE3 =  100.

APPEND LINE.

LINE-LINE1 = 2.

LINE-LINE2 = 20.

LINE-LINE3 = 200.

APPEND LINE.

LINE-LINE1 = 3.

LINE-LINE2 = 30.

LINE-LINE3 = 300.

APPEND LINE.

LINE-LINE1 = 4.

LINE-LINE2 = 40.

LINE-LINE3 = 400.

APPEND LINE.

* This is the internal table name passing as a parameter.

INT_NAME = 'line'.

PERFORM COMPUTE_IT USING (INT_NAME) FUN_NAME SORT1 SORT2.

*---------------------------------------------------------------------*

*       FORM compute_it                                               *

*---------------------------------------------------------------------*

*       ........                                                      *

*---------------------------------------------------------------------*

*  -->  PINT_NAME                                                     *

*  -->  PFUN_NAME                                                     *

*  -->  PSORT1                                                        *

*  -->  PSORT2                                                        *

*---------------------------------------------------------------------*

FORM COMPUTE_IT USING (PINT_NAME) LIKE INT_NAME PFUN_NAME  PSORT1

PSORT2  .

  WRITE : PINT_NAME.

  LOOP AT PINT_NAME.

    WRITE: LINE-LINE1.

    WRITE: LINE-LINE2.

    WRITE: LINE-LINE3.

  ENDLOOP.

ENDFORM.

Read only

Former Member
0 Likes
3,594

Sorry Preetam.. I misunderstood your requirement.

You can pass like this

http://searchsap.techtarget.com/answer/How-to-dynamically-pass-an-internal-table-name-thru-a-subrout...

SuDEESH

Read only

Former Member
Read only

0 Likes
3,594

Report is not working!

Read only

0 Likes
3,594

Not working

Read only

Former Member
0 Likes
3,594

This should work (if I understood you correctly) :

FIELD-SYMBOLS: <fs_structure>  TYPE ANY TABLE.

Data: lv_ref              TYPE REF TO data,

       lv_table type string.                

lv_table = 'MARA'.

   CREATE DATA lv_ref TYPE table of (lv_table).

   ASSIGN lv_ref->* TO <fs_structure>.

  select * from (lv_table)

    into TABLE <fs_structure>.

Read only

basarozgur_kahraman
Contributor
0 Likes
3,594

Hi Preetam,

Yesterday i published a blog about executing sql statements directly in sap.

i use dynamic program generation and dynamic table there.

You can check code.

https://scn.sap.com/community/abap/blog/2013/10/09/zsql-a-tool-to-execute-sql-statements-directly

Regards

Basar Ozgur

Read only

PeterJonker
Active Contributor
0 Likes
3,594

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

Read only

0 Likes
3,594

Really very thanks! However, I was not looking for this

Read only

Former Member
0 Likes
3,594

Hi Preetam,

I've used references (pointers) instead of normal parameters with predefined types.


REPORT ztesla.

"----------------------------------------

DATA: gt_tm1 TYPE STANDARD TABLE OF mara,

      gv_ref TYPE REF TO data.

"----------------------------------------

START-OF-SELECTION.

  SELECT * INTO TABLE gt_tm1 FROM mara UP TO 7 ROWS.

  GET REFERENCE OF gt_tm1 INTO gv_ref.

  PERFORM f_doit USING gv_ref '7'.

*&------------------------------------------*

*&      Form  f_doit

*&------------------------------------------*

FORM f_doit USING p_ref     TYPE REF TO data

                  p_option  TYPE char02.

  "········································

  DATA: lv_ref  TYPE REF TO data,

        lt_mara TYPE STANDARD TABLE OF mara.

  "········································

  FIELD-SYMBOLS: <lfs_tmara> TYPE ANY TABLE.

  ASSIGN p_ref->* TO <lfs_tmara>.

  "········································

  CASE p_option.

    WHEN '7'.

      REFRESH lt_mara. SELECT * INTO TABLE lt_mara FROM mara UP TO 10 ROWS.

      GET REFERENCE OF lt_mara INTO lv_ref.

      PERFORM f_doit USING lv_ref '99'.

    WHEN '99'.

      PERFORM f_show_alv USING p_ref.

    WHEN OTHERS.

  ENDCASE.

  "········································

  " You may use the next sentence to get/modify the data: (*)

  " assign component of + field-symbols (*)

ENDFORM.                    "f_doit

*&------------------------------------------*

*&      Form  f_show_alv

*&------------------------------------------*

FORM f_show_alv USING p_table_ref TYPE any.

  DATA: lo_alv TYPE REF TO cl_salv_table.

  FIELD-SYMBOLS: <lfs_table> TYPE ANY TABLE.

  ASSIGN p_table_ref->* TO <lfs_table>.

  TRY.

      CALL METHOD cl_salv_table=>factory

        IMPORTING

          r_salv_table = lo_alv

        CHANGING

          t_table     = <lfs_table>.

    CATCH cx_salv_msg.

  ENDTRY.

  lo_alv->display( ).

ENDFORM.                    "f_show_alv

(*)


FIELD-SYMBOLS: <lfs_field> type any,

                <lfs_struct> type any.

* assign ... to <lfs_struct>

ASSIGN COMPONENT 'MATNR' of STRUCTURE <lfs_struct> to <lfs_field>.

<lfs_field> = ...

Regards,

Luis