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

Create Table Structure Dinamically

Former Member
0 Likes
1,043

Hi,

We need to create an internal table dinamically in a program as we don´t know which fields it must contain until run time, how can this be done?.

Best Regards,

Jose

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,006

Hi Caballero,

Just click here for complete details about Dynamic Internal table:

http://www.saptechnical.com/Tutorials/ABAP/DynamicInternaltable/DynamicInternalTable.htm

Reward,if helpful.

Regards,

V.Raghavender.

9 REPLIES 9
Read only

Former Member
0 Likes
1,006

by using field symbols , please press f1 on field-symbols in se38 .

sample code like this

<b>data : loop_itc(11) value 'IA586-ITC01',

loop_pin(11) value 'IA586-PIN01',

loop_ain(11) value 'IA586-AIN01',

index(2) type p value 1,

unpacked_index(2).

field-symbols: <itc>, <pin> , <ain>.

loop at p586.

unpack index to unpacked_index.

loop_itc+09(2) = unpacked_index.

loop_pin+09(2) = unpacked_index.

loop_ain+09(2) = unpacked_index.

assign (loop_itc) to <itc>.

assign (loop_pin) to <pin>.

assign (loop_ain) to <ain>.

<itc> = p586-icode.

<pin> = p586-pinvt.

<ain> = p586-ainvt.

add 1 to index.

endloop.</b>

Regards

Prabhu

Read only

Former Member
0 Likes
1,006

Hi..

go through this thread..

<b>

Read only

santhosh_patil
Contributor
0 Likes
1,006

Hi,

Check out this report:

REPORT zcdf_dynamic_table.

  • Dynamic ALV Grid with Cell Coloring.

  • Build a field catalog dynamically and provide the ability to color

  • the cells.

  • To test, copy this code to any program name and create screen 100

  • as described in the comments. After the screen is displayed, hit

  • enter to exit the screen.

  • Tested in 4.6C and 6.20

  • Charles Folwell - cfolwell@csc.com - Feb 2, 2005

DATA:

r_dyn_table TYPE REF TO data,

r_wa_dyn_table TYPE REF TO data,

r_dock_ctnr TYPE REF TO cl_gui_docking_container,

r_alv_grid TYPE REF TO cl_gui_alv_grid,

t_fieldcat1 TYPE lvc_t_fcat, "with cell color

t_fieldcat2 TYPE lvc_t_fcat, "without cell color

wa_fieldcat LIKE LINE OF t_fieldcat1,

wa_cellcolors TYPE LINE OF lvc_t_scol,

wa_is_layout TYPE lvc_s_layo.

FIELD-SYMBOLS:

<t_dyn_table> TYPE STANDARD TABLE,

<wa_dyn_table> TYPE ANY,

<t_cellcolors> TYPE lvc_t_scol,

<w_field> TYPE ANY.

START-OF-SELECTION.

  • Build field catalog based on your criteria.

wa_fieldcat-fieldname = 'FIELD1'.

wa_fieldcat-inttype = 'C'.

wa_fieldcat-outputlen = '10'.

wa_fieldcat-coltext = 'My Field 1'.

wa_fieldcat-seltext = wa_fieldcat-coltext.

APPEND wa_fieldcat TO t_fieldcat1.

wa_fieldcat-fieldname = 'FIELD2'.

wa_fieldcat-inttype = 'C'.

wa_fieldcat-outputlen = '10'.

wa_fieldcat-coltext = 'My Field 2'.

wa_fieldcat-seltext = wa_fieldcat-coltext.

APPEND wa_fieldcat TO t_fieldcat1.

  • Before adding cell color table, save fieldcatalog to pass

  • to ALV call. The ALV call needs a fieldcatalog without

  • the internal table for cell coloring.

t_fieldcat2[] = t_fieldcat1[].

  • Add cell color table.

  • CALENDAR_TYPE is a structure in the dictionary with a

  • field called COLTAB of type LVC_T_SCOL. You can use

  • any structure and field that has the type LVC_T_SCOL.

wa_fieldcat-fieldname = 'T_CELLCOLORS'.

wa_fieldcat-ref_field = 'COLTAB'.

wa_fieldcat-ref_table = 'CALENDAR_TYPE'.

APPEND wa_fieldcat TO t_fieldcat1.

  • Create dynamic table including the internal table

  • for cell coloring.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = t_fieldcat1

IMPORTING

ep_table = r_dyn_table

EXCEPTIONS

generate_subpool_dir_full = 1

OTHERS = 2.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

  • Get access to new table using field symbol.

ASSIGN r_dyn_table->* TO <t_dyn_table>.

  • Create work area for new table.

CREATE DATA r_wa_dyn_table LIKE LINE OF <t_dyn_table>.

  • Get access to new work area using field symbol.

ASSIGN r_wa_dyn_table->* TO <wa_dyn_table>.

  • Get data into table from somewhere. Field names are

  • known at this point because field catalog is already

  • built. Read field names from the field catalog or use

  • COMPONENT <number> in a DO loop to access the fields. A

  • simpler hard coded approach is used here.

ASSIGN COMPONENT 'FIELD1' OF STRUCTURE <wa_dyn_table> TO <w_field>.

<w_field> = 'ABC'.

ASSIGN COMPONENT 'FIELD2' OF STRUCTURE <wa_dyn_table> TO <w_field>.

<w_field> = 'XYZ'.

APPEND <wa_dyn_table> TO <t_dyn_table>.

ASSIGN COMPONENT 'FIELD1' OF STRUCTURE <wa_dyn_table> TO <w_field>.

<w_field> = 'TUV'.

ASSIGN COMPONENT 'FIELD2' OF STRUCTURE <wa_dyn_table> TO <w_field>.

<w_field> = 'DEF'.

APPEND <wa_dyn_table> TO <t_dyn_table>.

  • Color cells based on your criteria. In this example, a test on

  • FIELD2 is used to decide on color.

LOOP AT <t_dyn_table> INTO <wa_dyn_table>.

ASSIGN COMPONENT 'FIELD2' OF STRUCTURE <wa_dyn_table> TO <w_field>.

  • Get access to internal table used to color cells.

ASSIGN COMPONENT 'T_CELLCOLORS'

OF STRUCTURE <wa_dyn_table> TO <t_cellcolors>.

CLEAR wa_cellcolors.

wa_cellcolors-fname = 'FIELD2'.

IF <w_field> = 'DEF'.

wa_cellcolors-color-col = '7'.

ELSE.

wa_cellcolors-color-col = '5'.

ENDIF.

APPEND wa_cellcolors TO <t_cellcolors>.

MODIFY <t_dyn_table> FROM <wa_dyn_table>.

ENDLOOP.

  • Display screen. Define screen 100 as empty, with next screen

  • set to 0 and flow logic of:

*

  • PROCESS BEFORE OUTPUT.

  • MODULE initialization.

*

  • PROCESS AFTER INPUT.

CALL SCREEN 100.

----


  • MODULE initialization OUTPUT

----


MODULE initialization OUTPUT.

  • Set up for ALV display.

IF r_dock_ctnr IS INITIAL.

CREATE OBJECT r_dock_ctnr

EXPORTING

side = cl_gui_docking_container=>dock_at_left

ratio = '90'.

CREATE OBJECT r_alv_grid

EXPORTING i_parent = r_dock_ctnr.

  • Set ALV controls for cell coloring table.

wa_is_layout-ctab_fname = 'T_CELLCOLORS'.

  • Display.

CALL METHOD r_alv_grid->set_table_for_first_display

EXPORTING

is_layout = wa_is_layout

CHANGING

it_outtab = <t_dyn_table>

it_fieldcatalog = t_fieldcat2.

ELSE. "grid already prepared

  • Refresh display.

CALL METHOD r_alv_grid->refresh_table_display

EXPORTING

i_soft_refresh = ' '

EXCEPTIONS

finished = 1

OTHERS = 2.

ENDIF.

ENDMODULE. " initialization OUTPUT

-


santhosh

Read only

Former Member
0 Likes
1,006

Hi,

Declare an internal table with STRING as single field and use it.

In run time data will come into this in a single line.

Then segregate it to individual fields with some logic based on the final data that will enter into this internal table.

reward if useful

regards,

Anji

Read only

Former Member
0 Likes
1,006

Hi,

Try the following code n let me know if it meets ur requirement.

PARAMETERS : p_table(10) TYPE c.

FIELD-SYMBOLS:

<dyn_table> TYPE STANDARD TABLE,

<dyn_wa>,

<dyn_field>.

DATA new_line TYPE REF TO data.

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.

CREATE DATA w_dref TYPE TABLE OF (w_tabname).

ASSIGN w_dref->* TO <t_itab>.

CREATE DATA new_line LIKE LINE OF <t_itab>.

ASSIGN new_line->* TO <dyn_wa>.

  • Populating Dynamic internal table

SELECT *

FROM (w_tabname) UP TO 100 ROWS

INTO TABLE <t_itab>.

  • Displaying dynamic internal table using Grid.

LOOP AT <t_itab> INTO <dyn_wa>.

DO.

ASSIGN COMPONENT sy-index OF STRUCTURE <dyn_wa> TO <dyn_field>.

IF sy-subrc <> 0.

EXIT.

ENDIF.

IF sy-index = 1.

WRITE:/ <dyn_field>.

ELSE.

WRITE: <dyn_field>.

ENDIF.

ENDDO.

ENDLOOP.

regards,

kiran kumar k.

Read only

Former Member
0 Likes
1,007

Hi Caballero,

Just click here for complete details about Dynamic Internal table:

http://www.saptechnical.com/Tutorials/ABAP/DynamicInternaltable/DynamicInternalTable.htm

Reward,if helpful.

Regards,

V.Raghavender.

Read only

Former Member
0 Likes
1,006

Hi,

Look this sample code: <a href="https://wiki.sdn.sap.com/wiki/display/Snippets/DynamicInternalTable">Dynamic Internal Table</a>.

Regards,

Read only

Former Member
0 Likes
1,006

Create a Dynamic Internal table .Just chek out this program .

type-pools : abap.

field-symbols: <dyn_table> type standard table,

<dyn_wa>,

<dyn_field>.

data: dy_table type ref to data,

dy_line type ref to data,

xfc type lvc_s_fcat,

ifc type lvc_t_fcat.

selection-screen begin of block b1 with frame.

parameters: p_table(30) type c default 'T001'.

selection-screen end of block b1.

start-of-selection.

perform get_structure.

perform create_dynamic_itab. *********Creates a dyanamic internal table*********

perform get_data.

perform write_out.

form get_structure.

data : idetails type abap_compdescr_tab,

xdetails type abap_compdescr.

data : ref_table_des type ref to cl_abap_structdescr.

  • Get the structure of the table.

ref_table_des ?=

cl_abap_typedescr=>describe_by_name( p_table ).

idetails[] = ref_table_des->components[].

loop at idetails into xdetails.

clear xfc.

xfc-fieldname = xdetails-name .

xfc-datatype = xdetails-type_kind.

xfc-inttype = xdetails-type_kind.

xfc-intlen = xdetails-length.

xfc-decimals = xdetails-decimals.

append xfc to ifc.

endloop.

endform.

form create_dynamic_itab.

  • Create dynamic internal table and assign to FS

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = ifc

importing

ep_table = dy_table.

assign dy_table->* to <dyn_table>.

  • Create dynamic work area and assign to FS

create data dy_line like line of <dyn_table>.

assign dy_line->* to <dyn_wa>.

endform.

form get_data.

  • Select Data from table.

select * into table <dyn_table>

from (p_table).

endform.

Write out data from table.

loop at <dyn_table> into <dyn_wa>.

do.

assign component sy-index

of structure <dyn_wa> to <dyn_field>.

if sy-subrc <> 0.

exit.

endif.

if sy-index = 1.

write:/ <dyn_field>.

else.

write: <dyn_field>.

endif.

enddo.

endloop.

Rusidar

Read only

Former Member
0 Likes
1,006

you can create dynamic table using field symbol.

and by calling floowing method:

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = lt

IMPORTING

ep_table = <fs_data>

EXCEPTIONS

generate_subpool_dir_full = 1

OTHERS = 2.

regards,

nazeer

reward if useful