Application Development 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: 

Declaring variable dynamically

Former Member
0 Kudos

Hi all,

I want to accept a table name, and declare an internal table which is of type of that accepted table.

12 REPLIES 12

former_member210123
Active Participant
0 Kudos

Use type any.Type any is used when the type is unknown.

0 Kudos

Dont forget it should be a field symbol or it is not possible.

Example field symbols test type any.

Then u can assign it to any thing.

0 Kudos

field symbol should be assigned to an internal table here, how do i declare that internal table whose type is unknown

0 Kudos

field symbol should be assigned to an internal table here, how do i declare that internal table whose type can not determined statically? the user will enter the type

0 Kudos

You can do that using the method of class

cl_alv_table_create method is create_dynamic_table

REPORT  Z_TEST_09.

PARAMETERS : p_table(10) TYPE C.

DATA:
  i_newtable  TYPE REF TO data.        " To create dyn.Itab
  DATA: w_tabname TYPE w_tabname,
        w_dref TYPE REF TO data,
        w_grid TYPE REF TO cl_gui_alv_grid.

  FIELD-SYMBOLS: <t_itab> TYPE ANY TABLE.

  w_tabname = p_table.

CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      i_style_table = 'SPFLI'
    IMPORTING
      ep_table        = i_newtable.

 ASSIGN i_newtable->* TO <t_itab>.
  SELECT *
    FROM (w_tabname) UP TO 20 ROWS
    INTO TABLE <t_itab>.

CREATE OBJECT w_grid
    EXPORTING i_parent = cl_gui_container=>screen0.

  CALL METHOD w_grid->set_table_for_first_display
EXPORTING
      i_structure_name = w_tabname
    CHANGING
      it_outtab        = <t_itab>.

0 Kudos

Hi Yash,

its giving syntax error, can u please have a look at it?

Former Member
0 Kudos

HI

Use field symbols

field-symbols: <fs1>

you can assign any type to field symbol at run time

Regards

Aditya

former_member188685
Active Contributor
0 Kudos

you can use the below syntax.

field-symbols: <fs> type any.

data: var(9).

var = 'IT_VBAP[]'.

assign (var) to <fs>.

There are many ways to do this. Above is one such.

0 Kudos

You can also do this...

data: tab type ref to data.

DATA: it_vabp TYPE STANDARD TABLE OF vbap WITH HEADER LINE.

select * from vbap into table it_vabp .

GET REFERENCE OF it_vabp[] INTO tab.

Former Member
0 Kudos

Hi

You can use

Field-symbols: <fs_tab> type any table.

This filed symbol then can accept any table dynamically.

you can process this dynamically filled table by statement

Loop at <fs_tab> into <wa_tab>.

loop at <wa_tab> assigning componets of <wa_tab> to <field>

endloop.

endloop.

This way you process any table dynamically.

Reward points if helpful

Regards,

Rahul

0 Kudos

I want to know how to fill the internal table dynamically?

Former Member
0 Kudos

Hi,

Do use the code. I think it will give you some picture.

&----


*& Report ZDYNTEST *

*& *

&----


*& *

*& *

&----


report zdyntest.

type-pools : slis.

tables : marc,t001w.

data : begin of itab occurs 0,

matnr like marc-matnr,

end of itab.

data : fieldstab type lvc_t_fcat,

stab type lvc_s_fcat,

t_fieldcat type slis_t_fieldcat_alv,

s_fieldcat type slis_fieldcat_alv,

new_line type ref to data,

new_table type ref to data,

index(3) type c,

str(70),

text(6),

cnt(1),

text1(16),

repid like sy-repid.

field-symbols : <fs> type standard table,

<wa> type any.

select-options : s_werks for marc-werks no intervals,

s_matnr for marc-matnr no intervals.

initialization.

repid = sy-repid.

start-of-selection.

select * from marc

into corresponding fields of table itab

where matnr in s_matnr

and werks in s_werks.

sort itab by matnr.

delete adjacent duplicates from itab comparing matnr.

stab-fieldname = 'MATNR'.

stab-datatype = 'CHAR'.

stab-intlen = '18'.

append stab to fieldstab.

clear cnt.

loop at s_werks.

clear text.

cnt = cnt + 1.

concatenate 'EISLO' cnt into text.

stab-fieldname = text.

stab-datatype = 'CHAR'.

stab-intlen = '16'.

append stab to fieldstab.

clear s_werks.

endloop.

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = fieldstab

importing

ep_table = new_table.

assign new_table->* to <fs>.

create data new_line like line of <fs>.

assign new_line->* to <wa>.

perform move_data.

clear s_fieldcat.

s_fieldcat-fieldname = 'MATNR'.

s_fieldcat-tabname = itab.

s_fieldcat-seltext_m = 'Part Number'.

s_fieldcat-no_zero = 'X'.

s_fieldcat-ddictxt = 'M'.

append s_fieldcat to t_fieldcat.

clear cnt.

loop at s_werks.

clear t001w.

cnt = cnt + 1.

select single * from t001w where werks = s_werks-low and spras = sy-langu.

clear text.

concatenate 'EISLO' cnt into text.

s_fieldcat-fieldname = text.

s_fieldcat-seltext_m = t001w-name2.

  • S_FIELDCAT-NO_ZERO = 'X'.

s_fieldcat-ddictxt = 'M'.

append s_fieldcat to t_fieldcat.

clear s_werks.

endloop.

call function 'REUSE_ALV_GRID_DISPLAY'

exporting

it_fieldcat = t_fieldcat[]

tables

t_outtab = <fs>.

&----


*& Form MOVE_DATA

&----


  • text

----


form move_data.

loop at itab.

clear str.

concatenate itab-matnr ' ' into str separated by space.

loop at s_werks.

clear marc.

select single * from marc where matnr = itab-matnr and werks = s_werks-low.

if sy-subrc eq 0.

clear text1.

text1 = marc-eislo.

concatenate str text1 into str separated by space.

else.

concatenate str '0' into str separated by space.

endif.

endloop.

<wa> = str.

append <wa> to <fs>.

clear itab.

endloop.

endform. "MOVE_DATA

Regards,

Sankar.