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

Declaring internal table names dynamically

Former Member
0 Likes
1,433

hi,

Iam selecting the tablenames from dd03l so based on the table names i have to declare internal table names.

select tabname from dd03l into t_dd03l

where tabname in s_tabname.

loop at t_dd03l

if t_dd03l-tabname = 'MARA'.

then i want to declare a internal table T_MARA type standard table of MARA.

or can i declare multiple dynamic internal table names where dynamic internal table name should keep changing at runtime based on the dd03l-tabname.

endloop.

4 REPLIES 4
Read only

Former Member
0 Likes
702

hi,

this example may help u.....

    • Example: how to create a dynamic internal table

**

  • The dynamic internal table stucture

DATA: BEGIN OF STRUCT OCCURS 10,

FILDNAME(8) TYPE C,

ABPTYPE TYPE C,

LENGTH TYPE I,

END OF STRUCT.

DATA: itab TYPE STANDARD TABLE OF string,

mess TYPE string,

lin TYPE i,

wrd TYPE string,

dir TYPE trdir.

  • The dynamic program source table

DATA: BEGIN OF INCTABL OCCURS 10,

LINE(72),

END OF INCTABL.

DATA: LNG TYPE I, TYPESRTING(6).

  • Sample dynamic internal table stucture

STRUCT-FILDNAME = 'field1'. STRUCT-ABPTYPE = 'c'. STRUCT-LENGTH = '6'.

APPEND STRUCT. CLEAR STRUCT.

STRUCT-FILDNAME = 'field2'. STRUCT-ABPTYPE = 'd'.

APPEND STRUCT. CLEAR STRUCT.

*STRUCT-FILDNAME = 'field3'. STRUCT-ABPTYPE = 'i'.

*APPEND STRUCT. CLEAR STRUCT.

  • Create the dynamic internal table definition in the dyn. program

INCTABL-LINE = 'Report zdynpro.'. APPEND INCTABL.

INCTABL-LINE = 'data: begin of dyntab occurs 10,'. APPEND INCTABL.

LOOP AT STRUCT.

INCTABL-LINE = STRUCT-FILDNAME.

LNG = STRLEN( STRUCT-FILDNAME ).

IF NOT STRUCT-LENGTH IS INITIAL .

TYPESRTING(1) = '('.

TYPESRTING+1 = STRUCT-LENGTH.

TYPESRTING+5 = ')'.

CONDENSE TYPESRTING NO-GAPS.

INCTABL-LINE+LNG = TYPESRTING.

ENDIF.

INCTABL-LINE+15 = 'type '.

INCTABL-LINE+21 = STRUCT-ABPTYPE.

INCTABL-LINE+22 = ','.

APPEND INCTABL.

ENDLOOP.

INCTABL-LINE = 'end of dyntab. '.

APPEND INCTABL.

  • Create the code processes the dynamic internal table

INCTABL-LINE = ' '. APPEND INCTABL.

INCTABL-LINE = 'dyntab-field1 = ''aaaaaa''.'. APPEND INCTABL.

INCTABL-LINE = 'dyntab-field2 = ''19970814''.'. APPEND INCTABL.

INCTABL-LINE = 'append dyntab.'. APPEND INCTABL.

INCTABL-LINE = ' '. APPEND INCTABL.

INCTABL-LINE = 'loop at dyntab.'. APPEND INCTABL.

INCTABL-LINE = 'write: / dyntab.'. APPEND INCTABL.

INCTABL-LINE = 'endloop.'. APPEND INCTABL.

INSERT REPORT 'zdynpro'(001) FROM INCTABL.

if sy-subrc eq 0.

SUBMIT ZDYNPRO.

endif.

<b></b>

regards,

Naresh.

Read only

0 Likes
702

Hi,

Iam sending a my program please suggest me where in the program i can create multiple dynamic internal tables.

TYPE-POOLS: SLIS.

TYPES: BEGIN OF X_DD03L,

TABNAME TYPE TABNAME,

END OF X_DD03L.

DATA: T_DD03L TYPE STANDARD TABLE OF X_DD03L,

W_DD03L TYPE X_DD03L.

DATA: G_TABNAME TYPE DD03L-TABNAME,

g_FIELDNAME type FIELDNAME,

T_FIELDCAT TYPE lvc_t_fcat,

w_fieldcat type LVC_S_FCAT,

new_table TYPE REF TO data,

l_wa_newline TYPE REF TO data,

p_table type char30.

types: begin of x_tab.

include structure PA0000.

types: end of x_tab.

data: t_tab type standard table of x_tab,

w_tab type x_tab,

l_tabix type sy-tabix.

*Field-symbols:

FIELD-SYMBOLS : <l_fs_dyn_table> TYPE STANDARD TABLE ,

<l_fs_dyn_wa> ,

<f_tab> type standard table,

<dyn_field>.

SELECT-OPTIONS:S_TABNAM FOR G_TABNAME,

s_Fnam for g_FIELDNAME.

SELECT TABNAME

FROM DD03L

INTO TABLE T_DD03L

WHERE TABNAME IN S_TABNAM.

delete adjacent duplicates from t_dd03l.

LOOP AT T_DD03L INTO W_DD03L.

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'

EXPORTING

i_structure_name = W_DD03L-TABNAME

CHANGING

ct_fieldcat = t_fieldcat

EXCEPTIONS

inconsistent_interface = 1

program_error = 2

OTHERS = 3.

IF sy-subrc <> 0.

*No sy-subrc required

ENDIF.

  • Create dynamic internal table and assign to FS

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = t_fieldcat

IMPORTING

ep_table = new_table.

ASSIGN new_table->* TO <l_fs_dyn_table>.

  • Create dynamic work area and assign to FS

CREATE DATA l_wa_newline LIKE LINE OF <l_fs_dyn_table>.

ASSIGN l_wa_newline->* TO <l_fs_dyn_wa>.

p_table = w_dd03l-tabname .

select * up to 10 rows into corresponding fields of table <l_fs_dyn_table>

from (p_table)

where pernr = '00000007'.

here we are creating <l_fs_dyn_table> dynamic table but every time when new tablename comes i want to change the dynamic table name . like <l_fs_dyn_table1> , <l_fs_dyn_table2> etc....

Read only

Former Member
0 Likes
702

Hi,

u can use the funtion module "rss_template_instantiate" for creating the program dynamically.see the doccumentation of that.

if u have any queries let me know.

regards,

bharat.

Read only

Former Member
0 Likes
702

Hi,

chk this code:

method GET_ITABLE_OF_TYPE_NAME .

*IMPORTING IM_ITABLE_TYPE_NAME TYPE STRING

*RETURNING RE_ITABLE_REF_TO_DATA type ref to data

DATA:

alv_field_catalog TYPE lvc_t_fcat,

alv_field LIKE LINE OF alv_field_catalog,

curr_col_pos LIKE alv_field-col_pos,

tmp_itable_type_name TYPE DD02L-TABNAME.

curr_col_pos = 1.

*the interface of function "LVC_FIELDCATALOG_MERGE" expect the name with this type

tmp_itable_type_name = im_itable_type_name.

*creating fieldcatalog

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'

EXPORTING

i_structure_name = tmp_itable_type_name

CHANGING

ct_fieldcat = alv_field_catalog

EXCEPTIONS

inconsistent_interface = 1

program_error = 2

OTHERS = 3.

IF sy-subrc <> 0.

*here jour error handling

ENDIF.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = alv_field_catalog

IMPORTING

ep_table = RE_ITABLE_REF_TO_DATA.

*Now ist in re_itable_ref_to_data a pointer to a internal table with the *wished type

endmethod.

To call the method:

Code:

DATA:

table_type_name type String,

ref_to_internal_table type ref to data.

FIELD-SYMBOLS : <internal_Table> type standard table.

table_type_name = 'LIKP'.

CALL METHOD ZCL_MYCLASS=>GET_ITABLE_OF_TYPE_NAME

EXPORTING

im_itable_type_name = table_type_name

receiving

re_itable_ref_to_data = ref_to_internal_table.

ASSIGN ref_to_internal_table->* to <internal_Table>.

SELECT * FROM (table_type_name) INTO CORRESPONDING FIELDS OF TABLE <internal_Table>.

etc. etc.

regards,

keerthi