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: 

how to create internal table dynamically

Former Member
0 Kudos

plz any can tell me how create internal table dynamically?

5 REPLIES 5

former_member404244
Active Contributor
0 Kudos

Hi,

check the below sample code for 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.

<b> xfc-intlen = xdetails-length / 2.

xfc-decimals = xdetails-decimals / 2.</b>

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.

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

Regards,

Nagaraj

Former Member
0 Kudos

Hi,

Check the following links:

https://www.sdn.sap.com/irj/sdn/wiki?path=/display/snippets/dynamic%2binternal%2btable

/people/rich.heilman2/blog/2005/07/27/dynamic-internal-tables-and-structures--abap

http://sap-img.com/ab030.htm

Regards,

Bhaskar

Former Member
0 Kudos

Hi,

Please look into the answered thread: -

Reward points for helpful answers.

Regards,

hari

former_member402443
Contributor
0 Kudos

Hello Rajesh,

Check the code below for creating dynamic internal table.

TYPE-POOLS: slis.

FIELD-SYMBOLS: <t_dyntable> TYPE STANDARD TABLE, " Dynamic internal table name

<fs_dyntable>, "Field symbol to create work area

<fs_fldval> type any. " Field symbol to assign values

PARAMETERS: p_cols(5) TYPE c. " Input number of columns

DATA: t_newtable TYPE REF TO data,

t_newline TYPE REF TO data,

lt_fldcat TYPE slis_t_fieldcat_alv,

t_fldcat TYPE lvc_t_fcat,

wa_it_fldcat TYPE lvc_s_fcat,

wa_colno(2) TYPE n,

wa_flname(5) TYPE c.

  • Create fields .

DO p_cols TIMES.

CLEAR wa_it_fldcat.

move sy-index to wa_colno.

concatenate 'COL'

wa_colno

into wa_flname.

wa_it_fldcat-fieldname = wa_flname.

wa_it_fldcat-datatype = 'CHAR'.

wa_it_fldcat-intlen = 10.

APPEND wa_it_fldcat TO t_fldcat.

ENDDO.

START-OF-SELECTION.

  • Create dynamic internal table and assign to FS

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = t_fldcat

IMPORTING

ep_table = t_newtable.

ASSIGN t_newtable->* TO <t_dyntable>.

  • Create dynamic work area and assign to FS

CREATE DATA t_newline LIKE LINE OF <t_dyntable>.

ASSIGN t_newline->* TO <fs_dyntable>.

Reward Points, if useful.

Regards,

Manoj Kumar

Former Member