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 internal table based on internal table fields

Sivakumar_Subramaniam
Product and Topic Expert
Product and Topic Expert
0 Likes
601

hi gurus.,

i need to declare an internal table dynamically based on the values(i.e field names) in another internal table ..

plz help me its very urgent ....

thanks in advance.,

s.sivakumar

5 REPLIES 5
Read only

former_member386202
Active Contributor
0 Likes
579

Hi,

Do like this

TYPES : BEGIN OF ty_src_file,

index(5) TYPE c.

INCLUDE TYPE ty_file_cpy.

TYPES : END OF ty_src_file.

Regards,

Prashant

Read only

Former Member
0 Likes
579
Read only

Former Member
0 Likes
579

Hi Look into the cpde below

REPORT ztest_dyn_it.

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.

END-OF-SELECTION.

*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 ne 0.

EXIT.

ENDIF.

IF sy-index = 1.

WRITE:/ <dyn_field>.

ELSE.

WRITE: <dyn_field>.

ENDIF.

ENDDO.

ENDLOOP.

Regards

Pavan

Read only

Former Member
0 Likes
579

hi check this ...

report z_dynamic.

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.

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.

&----


*& Form write_out

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


form write_out .

  • 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.

endform. " write_out

regards,

venkat.

Read only

Sivakumar_Subramaniam
Product and Topic Expert
Product and Topic Expert
0 Likes
579

hi gurus.,

my requirement is not satisfied .. i dont want to create an internal table based on the structure of a database table..

my *structureof dynamic table* only decided at runtime i.e., it is based on the fields names populated in my another internal table .. plz help me.. its urgent ...