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: 

Dynamic Internal Table

Former Member
0 Kudos

Hi,

Kindly give me code for generating dynamic internal table, with example.

pls dont giv me links.

With Rgds,

S.Barani

1 ACCEPTED SOLUTION

Former Member
0 Kudos

hi

good

check this code

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.

reward point if helpful.

thanks

mrutyun^

6 REPLIES 6

Former Member
0 Kudos

Hi S

REPORT ZDYNAMIC_TABLE LINE-SIZE 255.

FIELD-SYMBOLS : <GT_1> TYPE TABLE ,

<GS_1> TYPE ANY ,

  • <FS> TYPE ANY .

<F_1> TYPE ANY .

DATA: BEGIN OF GT_FLD OCCURS 0,

FIELDNAME TYPE FIELDNAME,

END OF GT_FLD.

DATA: BEGIN OF GT_1 OCCURS 0,

KUNNR LIKE BSID-KUNNR,

DMBTR LIKE BSID-DMBTR,

END OF GT_1.

DATA: GD_REF1 TYPE REF TO DATA,

GT_ALV_CAT TYPE TABLE OF LVC_S_FCAT WITH HEADER LINE.

START-OF-SELECTION.

REFRESH: GT_FLD.

REFRESH GT_ALV_CAT.

MOVE 'GJAHR' TO GT_FLD. APPEND GT_FLD.

MOVE 'BSID' TO GT_ALV_CAT-REF_TABLE.

MOVE 'GJAHR' TO GT_ALV_CAT-REF_FIELD.

MOVE 'GJAHR' TO GT_ALV_CAT-FIELDNAME.

APPEND GT_ALV_CAT.

MOVE 'KUNNR' TO GT_FLD. APPEND GT_FLD.

MOVE 'BSID' TO GT_ALV_CAT-REF_TABLE.

MOVE 'KUNNR' TO GT_ALV_CAT-REF_FIELD.

MOVE 'KUNNR' TO GT_ALV_CAT-FIELDNAME.

APPEND GT_ALV_CAT.

MOVE 'DMBTR' TO GT_FLD. APPEND GT_FLD.

MOVE 'BSID' TO GT_ALV_CAT-REF_TABLE.

MOVE 'DMBTR' TO GT_ALV_CAT-REF_FIELD.

MOVE 'DMBTR' TO GT_ALV_CAT-FIELDNAME.

APPEND GT_ALV_CAT.

  • make dynamic table to use alv method

CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE

EXPORTING IT_FIELDCATALOG = GT_ALV_CAT[]

IMPORTING EP_TABLE = GD_REF1.

ASSIGN GD_REF1->* TO <GT_1>.

SELECT (GT_FLD) INTO TABLE <GT_1>

FROM BSID

WHERE GJAHR = 2007 AND

BUKRS = 'C5A0'.

  • assign data to field

LOOP AT <GT_1> ASSIGNING <GS_1>.

CLEAR GT_1.

ASSIGN COMPONENT 'KUNNR' OF STRUCTURE <GS_1> TO <F_1>.

MOVE <F_1> TO GT_1-KUNNR.

ASSIGN COMPONENT 'DMBTR' OF STRUCTURE <GS_1> TO <F_1>.

MOVE <F_1> TO GT_1-DMBTR.

APPEND GT_1.

ENDLOOP.

Regards

Wiboon

Former Member
0 Kudos

REPORT zmaschl_create_data_dynamic .

TYPE-POOLS: slis.

DATA: it_fcat TYPE slis_t_fieldcat_alv,

is_fcat LIKE LINE OF it_fcat.

DATA: it_fieldcat TYPE lvc_t_fcat,

is_fieldcat LIKE LINE OF it_fieldcat.

DATA: new_table TYPE REF TO data.

DATA: new_line TYPE REF TO data.

FIELD-SYMBOLS: <l_table> TYPE ANY TABLE,

<l_line> TYPE ANY,

<l_field> TYPE ANY.

  • Build fieldcat

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

i_structure_name = 'SYST'

CHANGING

ct_fieldcat = it_fcat[].

LOOP AT it_fcat INTO is_fcat WHERE NOT reptext_ddic IS initial.

MOVE-CORRESPONDING is_fcat TO is_fieldcat.

is_fieldcat-fieldname = is_fcat-fieldname.

is_fieldcat-ref_field = is_fcat-fieldname.

is_fieldcat-ref_table = is_fcat-ref_tabname.

APPEND is_fieldcat TO it_fieldcat.

ENDLOOP.

  • Create a new Table

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = it_fieldcat

IMPORTING

ep_table = new_table.

  • Create a new Line with the same structure of the table.

ASSIGN new_table->* TO <l_table>.

CREATE DATA new_line LIKE LINE OF <l_table>.

ASSIGN new_line->* TO <l_line>.

  • Test it...

DO 30 TIMES.

ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.

<l_field> = sy-index.

INSERT <l_line> INTO TABLE <l_table>.

ENDDO.

LOOP AT <l_table> ASSIGNING <l_line>.

ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.

WRITE <l_field>.

ENDLOOP.

***************************************************************************

Regards

vasu

Former Member
0 Kudos

hi

good

check this code

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.

reward point if helpful.

thanks

mrutyun^

varma_narayana
Active Contributor
0 Kudos

Hi..

find the Examples below:

To create internal table dynamically

Example 1:

PARAMETERS:DB_TABLE(30).

DATA FCAT1 TYPE LVC_T_FCAT.

DATA:DYN_ITAB TYPE REF TO DATA,

WA TYPE REF TO DATA.

FIELD-SYMBOLS: <DISP_TABLE> TYPE TABLE,

<WA> TYPE ANY.

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'

EXPORTING

I_STRUCTURE_NAME = DB_TABLE

CHANGING

CT_FIELDCAT = FCAT1[].

CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE

EXPORTING

IT_FIELDCATALOG = FCAT1[]

IMPORTING

EP_TABLE = DYN_ITAB.

ASSIGN DYN_ITAB->* TO <DISP_TABLE>.

CREATE DATA WA LIKE LINE OF <DISP_TABLE>.

ASSIGN WA->* TO <WA>.

SELECT * FROM (db_table) INTO <WA>.

APPEND <WA> TO <DISP_table>.

ENDSELECT.

DESCRIBE TABLE <DISP_table>.

WRITE:/ SY-TFILL.

*********************************************************************

Example 2:

DATA: L_TABNAME TYPE DD02L-TABNAME VALUE 'MARA'.

DATA : REF_TAB TYPE REF TO DATA.

FIELD-SYMBOLS: <FTAB> TYPE TABLE.

**Create the ITAB dynamically and Assign to Field symbol

CREATE DATA REF_TAB TYPE TABLE OF (L_TABNAME).

ASSIGN REF_TAB->* TO <FTAB>.

**Construct the Select statement dynamically..

SELECT * FROM (L_TABNAME) INTO TABLE <FTAB>.

DESCRIBE TABLE <FTAB>.

WRITE:/ SY-TFILL.

reward if Helpful.

Former Member
0 Kudos

HI,

I have changed the program,

i am getting this dump : <b>LOAD_PROGRAM_NOT_FOUND</b>

here my code :

REPORT ZSBN_DYNAMICITAB .

TYPE-POOLS: SLIS.

TABLES : YMANPOWER_TABLE.

DATA: IT_FCAT TYPE SLIS_T_FIELDCAT_ALV,

IS_FCAT LIKE LINE OF IT_FCAT.

DATA: IT_FIELDCAT TYPE LVC_T_FCAT,

IS_FIELDCAT LIKE LINE OF IT_FIELDCAT.

DATA: NEW_TABLE TYPE REF TO DATA.

DATA: NEW_LINE TYPE REF TO DATA.

&& ITAB'S.

DATA : BEGIN OF GIT_MANPOWER OCCURS 0.

INCLUDE TYPE YMANPOWER_TABLE.

DATA : END OF GIT_MANPOWER .

DATA : GIT_COLS LIKE GIT_MANPOWER OCCURS 0 WITH HEADER LINE,

GIT_ROWS LIKE GIT_MANPOWER OCCURS 0 WITH HEADER LINE.

FIELD-SYMBOLS: <L_TABLE> TYPE ANY TABLE,

<L_LINE> TYPE ANY,

<L_FIELD> TYPE ANY.

&**********

&& DATA FROM MANPOWER TABLE

SELECT *

FROM YMANPOWER_TABLE

INTO TABLE GIT_MANPOWER.

GIT_COLS[] = GIT_MANPOWER[].

&& FOR BUILDING COLOUMNS

SORT GIT_COLS ASCENDING BY BUKRS.

DELETE ADJACENT DUPLICATES FROM GIT_COLS COMPARING BUKRS.

&& BUILDIGN FCATS

IS_FIELDCAT-FIELDNAME = 'PERSK'.

IS_FIELDCAT-REF_FIELD = 'PERSK'.

IS_FIELDCAT-REF_TABLE = 'YMANPOWER_TABLE'.

APPEND IS_FIELDCAT TO IT_FIELDCAT.

CLEAR : IS_FIELDCAT.

LOOP AT GIT_COLS.

IS_FIELDCAT-FIELDNAME = GIT_COLS-WERKS.

IS_FIELDCAT-REF_FIELD = GIT_COLS-WERKS.

IS_FIELDCAT-REF_TABLE = 'YMANPOWER_TABLE'.

APPEND IS_FIELDCAT TO IT_FIELDCAT.

CLEAR : IS_FIELDCAT.

ENDLOOP.

  • Create a new Table

CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE

EXPORTING

IT_FIELDCATALOG = IT_FIELDCAT

IMPORTING

EP_TABLE = NEW_TABLE.

Kindly help me on this..

With Rgds,

S.Barani

uwe_schieferstein
Active Contributor
0 Kudos

Hello

Using class CL_ALV_TABLE_CREATE is the old-fashioned way where you will definitely run into trouble if you run the required method too frequently because this class uses subroutine pools which will dump after > 36 times of creating itabs.

Instead, use the modern <b>RTTI </b>classes. For details please refer to my sample reports <b>ZUS_SDN_RTTI_CREATE_STRUCT...</b> in thread

The sample reports not only show how to create flat but also <i>complex </i>itabs.

Regards

Uwe