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

Need to make the internal table structure dynamic

Former Member
0 Likes
1,166

Hi ,

I need to upload data from an excel file, the columns in the excel keep on varying

E.g the number of columns in one excel file can be 10, in the second excel file it can be 15.

So in order to upload the data into an internal table the structre of internal table needs to be dynamic and change according to the number of fields in the input file .

Is it possible by using field symbols or is there any other method to do the same

Thanks.

Nishant

Edited by: nishant patel on Sep 17, 2009 10:48 AM

1 ACCEPTED SOLUTION
Read only

awin_prabhu
Active Contributor
0 Likes
1,116

Hi Nishant,

According to ur requirement, dynamic internal table can be created like below. But database table name has to passed dynacimally.

DATA: ftab TYPE STANDARD TABLE OF ddfield,

ftab_wa TYPE ddfield,

fcat_itab TYPE lvc_t_fcat,

fcat_wa TYPE lvc_s_fcat,

poi_itab TYPE REF TO data.

FIELD-SYMBOLS: <itab> TYPE STANDARD TABLE.

CALL FUNCTION 'DD_NAMETAB_TO_DDFIELDS'

EXPORTING

tabname = p_table " Pass database table name dynamically

TABLES

ddfields = ftab.

LOOP AT ftab INTO ftab_wa.

fcat_wa-fieldname = ftab_wa-fieldname.

fcat_wa-ref_field = ftab_wa-fieldname.

fcat_wa-ref_table = p_table.

APPEND fcat_wa TO fcat_itab.

ENDLOOP.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = fcat_itab

IMPORTING

ep_table = poi_itab

EXCEPTIONS

generate_subpool_dir_full = 1

OTHERS = 2.

ASSIGN poi_itab->* TO <itab>. " <itab> is dynamic internal table

Might help u.

Thanks.

Edited by: Sap Fan on Sep 17, 2009 1:19 PM

Hi ,

I need to upload data from an excel file, the columns in the excel keep on varying

E.g the number of columns in one excel file can be 10, in the second excel file it can be 15.

So in order to upload the data into an internal table the structre of internal table needs to be dynamic and change according to the number of fields in the input file .

Is it possible by using field symbols or is there any other method to do the same

Thanks.

Nishant

Edited by: nishant patel on Sep 17, 2009 10:48 AM

8 REPLIES 8
Read only

Former Member
0 Likes
1,116

Try the following..........

PARAMETERS : p_table(10) TYPE C.

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_itab> type any.

w_tabname = p_table.

assign w_tabname to <w_itab>.

CREATE DATA w_dref TYPE TABLE OF (w_tabname).

ASSIGN w_dref->* TO <t_itab>.

SELECT *

FROM (w_tabname) UP TO 20 ROWS

INTO TABLE <t_itab>.

Read only

Former Member
0 Likes
1,116

Hi, if you make a structure with field eq FLD01, FLD02, FLD03 etc. you can use a FIELD SYMBOL.

Code is something like this:

FIELD-SYMBOLS: <fs_fld> TYPE any.

DATA: wa_cnt TYPE i,

wa_fieldname(8) TYPE c,

wa_num1(1) TYPE n,

wa_num2(2) TYPE n.

CLEAR wa_cnt.

WHLIE .......

wa_cnt = wa_cnt + 1.

wa_num2 = wa_cnt.

CONCATENATE 'REC-FLD' wa_num2 INTO wa_fieldname.

ASSIGN (wa_fieldname) TO <fs_fld>.

<fs_fld> = ..

ENDWHILE.

Success.

Read only

0 Likes
1,116

If you need to pass the final internal table to ALV try this...

DATA : li_fieldcat TYPE lvc_t_fcat,

lw_fieldcat LIKE LINE OF gi_fieldcat.

DATA: li_new_table TYPE REF TO data,

lw_new_line TYPE REF TO data.

FIELD-SYMBOLS: <fs_tab> TYPE STANDARD TABLE,

<fs_wa> TYPE ANY.

According to number of fields in excel first,build the field catalogue internal table to li_fieldcat

Then pass this internal table >

*/..Method to create dynamic internal table creation

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = li_fieldcat

IMPORTING

ep_table = li_new_table.

ASSIGN li_new_table->* TO <fs_tab>.

CREATE DATA lw_new_line LIKE LINE OF <fs_tab>.

*/..Workarea

ASSIGN lw_new_line->* TO <fs_wa>.

<fs_tab> will have the structure of the excel sheet fields.

Read only

Former Member
0 Likes
1,116
check out this code ->
START-OF-SELECTION.
write : / 'SPFLI', / 'SFLIGHT', / 'Sbook'.

at LINE-SELECTION.
  data: tabname TYPE tabname,
        dref TYPE REF TO data,
              alv TYPE REF TO cl_gui_alv_grid.
  
  FIELD-SYMBOLS : <itab> TYPE ANY TABLE.
  
  read CURRENT LINE LINE VALUE INTO tabname.
  CREATE DATA dref TYPE TABLE OF (tabname).
  ASSIGN dref->* to <itab>.
  
  CREATE OBJECT alv
       EXPORTING i_parent = cl_gui_container=>screen0.
  
  call METHOD alv->set_table_for_first_display
      EXPORTING i_structure_name = tabname
        CHANGING it_outtab        = <itab>.
  
  CALL SCREEN 100.

Edited by: Sourabh Jain on Sep 17, 2009 12:21 PM

Edited by: Sourabh Jain on Sep 18, 2009 12:25 PM

Read only

awin_prabhu
Active Contributor
0 Likes
1,117

Hi Nishant,

According to ur requirement, dynamic internal table can be created like below. But database table name has to passed dynacimally.

DATA: ftab TYPE STANDARD TABLE OF ddfield,

ftab_wa TYPE ddfield,

fcat_itab TYPE lvc_t_fcat,

fcat_wa TYPE lvc_s_fcat,

poi_itab TYPE REF TO data.

FIELD-SYMBOLS: <itab> TYPE STANDARD TABLE.

CALL FUNCTION 'DD_NAMETAB_TO_DDFIELDS'

EXPORTING

tabname = p_table " Pass database table name dynamically

TABLES

ddfields = ftab.

LOOP AT ftab INTO ftab_wa.

fcat_wa-fieldname = ftab_wa-fieldname.

fcat_wa-ref_field = ftab_wa-fieldname.

fcat_wa-ref_table = p_table.

APPEND fcat_wa TO fcat_itab.

ENDLOOP.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = fcat_itab

IMPORTING

ep_table = poi_itab

EXCEPTIONS

generate_subpool_dir_full = 1

OTHERS = 2.

ASSIGN poi_itab->* TO <itab>. " <itab> is dynamic internal table

Might help u.

Thanks.

Edited by: Sap Fan on Sep 17, 2009 1:19 PM

Read only

0 Likes
1,116

Hii SAP FAN,

i have to load the data from the flat file ,so like how will the internal table structure vary according to number of columns in the flat file

Thanks,

Nishant

Read only

0 Likes
1,116
Read only

former_member222860
Active Contributor
0 Likes
1,116

Here's demo ALV prg., close to your requirement.

BCALV_TABLE_CREATE