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

Dynamic Internal Table

Former Member
0 Likes
550

How to create dynamic internal table with sample code?

Please help me on this..

Thanks in advance.

Paul.

5 REPLIES 5
Read only

gopi_narendra
Active Contributor
0 Likes
523

Check this blog, its easily understood

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

Regards

Gopi

Read only

Former Member
0 Likes
523

Hi,

Please check with the below link. It gives few sample codes which explains the creation of dynamic internal tables.

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

Hope this helps you.

Reward if useful.

Regards

Sayee

Read only

Former Member
0 Likes
523

&----


*& Report Z03_DYN_TAB

*&

&----


*& Simple program which create the internal table

*&Dynamically

&----


REPORT z03_dyn_tab.

*report z_dynamic.

TABLES: makt.

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

&----


  • text

----


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. "get_structure

&----


*& Form create_dynamic_itab

&----


  • text

----


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. "create_dynamic_itab

&----


*& Form get_data

&----


  • text

----


FORM get_data.

  • Select Data from table.

SELECT * INTO TABLE <dyn_table>

FROM (p_table).

ENDFORM. "get_data

&----


*& 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

Read only

Former Member
0 Likes
523

HI,

see this two examples.

REPORT ZDYN_ITAB.

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.

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

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.

rgds,

bharat.

Read only

varma_narayana
Active Contributor
0 Likes
523

Hi Eswar.

This is the Sample code for Creating internal table dynamically based on a structure.

DATA : REF_TAB TYPE REF TO DATA.

FIELD-SYMBOLS: <FTAB> TYPE TABLE.

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

CREATE DATA REF_TAB TYPE TABLE OF (L_TABNAME).

ASSIGN REF_TAB->* TO <FTAB>.

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

DESCRIBE TABLE <FTAB>.

WRITE:/ SY-TFILL.

REWARD IF HELPFUL