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

Creating Dynamic table

Former Member
0 Likes
1,248

Hi,

I am uploading one file into my program with structure_name,field_name,field_value and records. Now I want to create a dynamic internal table with the above fields and want to fill the records too.Can anyone tell how to do it?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
851

Hi Ginger,

First you need create the field catalog with the structure fields you are uploading and pass this field catalog

to the Fm to create a dynamic internal table

FIELD-SYMBOLS: <itab1> TYPE STANDARD TABLE,

<wa_dref1>,

  • Create dynamic work area and assign to FS

CREATE DATA dy_line2 LIKE LINE OF <itab>.

ASSIGN dy_line2->* TO <wa_dref2>.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = tb_seg_fcat---> field catalog name

IMPORTING

ep_table = wa_dref1---> work Area

**-- Assign the work area to the dynamic internal table

ASSIGN wa_dref1->* TO <itab1>.

Thank you.

Regards,

Padmasri.

6 REPLIES 6
Read only

former_member194416
Contributor
0 Likes
851

Hi,

Check below routines for creating dynamic table. It's not the all code but it should give you the idea.

*&---------------------------------------------------------------------*
*&      Form  set_fieldcat
*&---------------------------------------------------------------------*
form set_fieldcat.

  ls_fieldcatalog-fieldname = 'LIFNR'.
  ls_fieldcatalog-coltext  =  'Satýcý'.

  append ls_fieldcatalog to lt_fieldcatalog  .

  ls_fieldcatalog-fieldname = 'NAME1'.
  ls_fieldcatalog-coltext  =  'Satýcý Metni'.
  ls_fieldcatalog-intlen   =  '30'.

  append ls_fieldcatalog to lt_fieldcatalog  .

  loop at zzmmse05.

    ls_fieldcatalog-fieldname = zzmmse05-tturu .
    ls_fieldcatalog-coltext  = zzmmse05-ttext.

    ls_fieldcatalog-inttype   = 'C'            .
    append ls_fieldcatalog to lt_fieldcatalog  .

  endloop.


endform.                    " set_fieldcat


*&---------------------------------------------------------------------*
*&      Form  dynamic_table_create
*&---------------------------------------------------------------------*
form dynamic_table_create.

* Create dynamic internal table and assign to FS
  call method cl_alv_table_create=>create_dynamic_table
               exporting
                  it_fieldcatalog =  lt_fieldcatalog
               importing
                  ep_table        = dy_table.

  assign dy_table->* to <dyn_table>.

* Create dynamic work area and assign to FS
  assign dy_line->* to <dyn_wa>.
  create data dy_line like line of <dyn_table>.


  assign dy_line->*  to <fs_2>.

endform.                    " dynamic_table_create
*&---------------------------------------------------------------------*
*&      Form  fill_dynamic_table
*&---------------------------------------------------------------------*
form fill_dynamic_table.
  data: name1 like lfa1-name1.

  loop at zzmmse01.
  select single name1 from lfa1 into name1 where lifnr = zzmmse01-lifnr.
    str = 'LIFNR'.
    pmove zzmmse01-lifnr str.
    str = 'NAME1'.
    pmove name1 str.

*pmove name1 str.

    loop at zzmmse05 where tturu = zzmmse01-tturu.
      str = zzmmse05-tturu.
      zzmmse01-tpuan = zzmmse01-tpuan / zzmmse01-count.
      pmove zzmmse01-tpuan str.
      clear str.
    endloop.
    at end of lifnr.
      append <fs_2> to <dyn_table>.
      clear <fs_2>.
    endat.
  endloop.
endform.                    " fill_dynamic_table

Read only

0 Likes
851

Hey thanks for the reply.My problem is I need to do it in a generic way....suppose I do not know the field names existing inside the file.Then how to create the table ?

Read only

0 Likes
851

If you do not know the fieldnames.....it means that you need one generic upload program which can upload data into any internal table...

Am i right?

If yes, then what you can do is create a database table.....which will hold the filenames...and the fieldnames....

Everytime...anyone has to upload data...he has to enter the filename and field names into this database table...

In the program...based on the file name, you have to retrieve the columsn for it...

This will ensure that you can create a dynamic intenal table with any number of columns..

Hope it gives you some idea!

Read only

Former Member
0 Likes
852

Hi Ginger,

First you need create the field catalog with the structure fields you are uploading and pass this field catalog

to the Fm to create a dynamic internal table

FIELD-SYMBOLS: <itab1> TYPE STANDARD TABLE,

<wa_dref1>,

  • Create dynamic work area and assign to FS

CREATE DATA dy_line2 LIKE LINE OF <itab>.

ASSIGN dy_line2->* TO <wa_dref2>.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = tb_seg_fcat---> field catalog name

IMPORTING

ep_table = wa_dref1---> work Area

**-- Assign the work area to the dynamic internal table

ASSIGN wa_dref1->* TO <itab1>.

Thank you.

Regards,

Padmasri.

Read only

0 Likes
851

Hello Ginger ,

Following are the steps:

1. Read file containing fields name into internal table. for example you can use GUI_UPLOAD for same.

2. Once you have all the fields ready in internal table then construct fieldcatlog using these fields

loop at internal table with all the fields.

move it to field catalog.

append field catalog.

endloop.

3. Pass this field catalog table to static method create_dynamic_table method

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = fieldcatalog_tab

importing

ep_table = table.

Here table is defined with data object type.

now assign table reference to field symbol of type table.

ASSIGN table->* to <field-tab>.

Also create work area <field-wa> using refrence of table.

create data object wa LIKE LINE OF <field-tab>.

ASSIGN wa->* to <field-wa>.

Also define field symbol for field name.

for e.g. <field_name>

4. Dynamic internal table is ready

5. To fill this dynamic internal table using ASSIGN COMPONENT <Comp_number> OF STRUCTURE <field-wa> TO <field-name>

So in this case if first field of structure STRUCT1 is user_id then sudo-code will be

loop at internal table containing list of fields into field_wa

ASSIGN COMPONENT field_wa OF STRUCTURE <field-wa> TO <field>. "Here field_wa is wa area for single column internal table holding all the fieldnames.

Now <field-name> points to user_id field. Move some value into it as nornally we do with variables.

move '001' to <field-name>.

or

<field-name> = '001'.

endloop.

after completing all the fields one row will be ready in <field_wa>.

APPEND <field_wa> to <field_tab>.

Hope this helps you.

Thanks,

Augustin.

Read only

venkat_o
Active Contributor
0 Likes
851

Hi,

Have a look at this sample program to create dynamic internal and passing data to that internal table.

<pre>

REPORT ztest_notepad.

&----


*& Declarations

&----


*Type-pools

TYPE-POOLS:

slis.

*Types

TYPES:

ty_fcat TYPE lvc_s_fcat,

ty_fcatalog TYPE slis_fieldcat_alv.

*Work areas

DATA:

wa_fcat TYPE ty_fcat,

wa_fcatalog TYPE ty_fcatalog.

*Internal tables

DATA:

it_fcat TYPE STANDARD TABLE OF ty_fcat,

it_fcatalog TYPE STANDARD TABLE OF ty_fcatalog.

*Type reference

DATA:

it_dyn_tab TYPE REF TO data,

wa_newline TYPE REF TO data.

*Filed symbols

FIELD-SYMBOLS:

<gt_table> TYPE STANDARD TABLE,

<fs_dyntable>,

<fs_fldval> TYPE ANY,

<l_field> TYPE ANY.

*Variables

DATA:

l_fieldname TYPE lvc_s_fcat-fieldname,

l_tabname TYPE lvc_s_fcat-tabname,

l_fieldtext TYPE lvc_s_fcat-seltext,

l_index TYPE char2.

"Selection-screen

PARAMETERS:

p_colms TYPE i.

&----


*& start-of-selection.

&----


START-OF-SELECTION.

PERFORM build_fieldcat.

PERFORM create_dynamic_table.

DO 20 TIMES.

DO p_colms TIMES.

l_index = sy-index.

CONCATENATE 'FIELD' l_index INTO l_fieldname.

ASSIGN COMPONENT l_fieldname OF STRUCTURE <fs_dyntable> TO <l_field>.

<l_field> = sy-index.

ENDDO.

INSERT <fs_dyntable> INTO TABLE <gt_table>.

ENDDO.

LOOP AT it_fcat INTO wa_fcat.

PERFORM fieldcatalog1 USING: wa_fcat-fieldname

wa_fcat-tabname

wa_fcat-seltext.

ENDLOOP.

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

i_callback_program = 'ZTEST_NOTEPAD'

it_fieldcat = it_fcatalog

TABLES

t_outtab = <gt_table>.

&----


*& Form BUILD_FIELDCAT

&----


FORM build_fieldcat .

CLEAR: l_fieldname,

l_tabname,

l_fieldtext,

l_index.

DO p_colms TIMES.

CLEAR l_index.

l_index = sy-index.

CONCATENATE 'FIELD' l_index INTO l_fieldname.

CONCATENATE 'Field' l_index INTO l_fieldtext.

l_tabname = '<GT_TABLE>'.

PERFORM fieldcatalog USING: l_fieldname

l_tabname

l_fieldtext.

ENDDO.

ENDFORM. " BUILD_FIELDCAT

&----


*& Form CREATE_DYNAMIC_TABLE

&----


FORM create_dynamic_table .

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = it_fcat

IMPORTING

ep_table = it_dyn_tab.

ASSIGN it_dyn_tab->* TO <gt_table>.

  • Create dynamic work area and assign to FS

CREATE DATA wa_newline LIKE LINE OF <gt_table>.

ASSIGN wa_newline->* TO <fs_dyntable>.

ENDFORM. " CREATE_DYNAMIC_TABLE

&----


*& Form FIELDCATALOG

&----


FORM fieldcatalog USING field table f_txt.

wa_fcat-fieldname = field.

wa_fcat-tabname = table.

wa_fcat-seltext = f_txt.

APPEND wa_fcat TO it_fcat.

CLEAR wa_fcat.

ENDFORM. " FIELDCATALOG

&----


*& Form FIELDCATALOG1

&----


FORM fieldcatalog1 USING field table f_txt.

wa_fcatalog-fieldname = field.

wa_fcatalog-tabname = table.

wa_fcatalog-seltext_m = f_txt.

APPEND wa_fcatalog TO it_fcatalog.

CLEAR wa_fcatalog.

ENDFORM. " FIELDCATALOG1 </pre>Thanks

Venkat.O