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 declaration of internal table

Former Member
0 Likes
902

Hi all,

Is there any way of declaring an internal table with varying no of fields in the table ? In other words i want to creat an internal table in which no of fields is not fixed.

regards

deepak

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
814

Hi Deepak,

Plz refer the links below,

Regards,

Hema.

    • Reward points if it is useful.

Hi all,

Is there any way of declaring an internal table with varying no of fields in the table ? In other words i want to creat an internal table in which no of fields is not fixed.

regards

deepak

6 REPLIES 6
Read only

Former Member
0 Likes
814

HI ,

<b>You need to create Dynamic Internal Table.</b>

Also have a look at below weblog:

/people/ravikumar.allampallam/blog/2005/05/31/expand-the-list-of-columns-in-a-report-dynamically

/people/subramanian.venkateswaran2/blog/2004/11/19/dynamic-internal-table

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

/people/subramanian.venkateswaran2/blog/2004/11/19/dynamic-internal-table%3Fpage%3Dlast%26x-showcontent%3Doff

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

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

http://searchsap.techtarget.com/tip/1,289483,sid21_gci912390,00.html

Also Copy/ Paste this code in SE38 and execute by giving Table name..It gives you info abt how to create dynamic internal table.

REPORT ZGILL_FS line-size 250

line-count 65 .

parameter p_table type tabname.

field-symbols <tab> type table.

field-symbols <tab1> type any.

types: begin of itab,

t_name type tabname,

t_ref type ref to data,

end of itab.

data itab1 type table of itab with non-unique key t_name.

perform fetch_data using p_table.

perform print_table using p_table.

&----


*& Form fetch_data

&----


  • text

----


  • -->P_P_TABLE text

----


FORM fetch_data USING P_TABLE1 type tabname.

data itab2 type itab.

itab2-t_name = p_table1.

create data itab2-t_ref type table of (itab2-t_name) .

assign itab2-t_ref->* to <tab>.

append itab2 to itab1.

select * from (p_table1) up to 25 rows into corresponding fields of table <tab>.

ENDFORM. " fetch_data

&----


*& Form print_table

&----


  • text

----


  • -->P_P_TABLE text

----


FORM print_table USING P_TABLE1 type tabname.

DATA t_ref1 TYPE REF TO data.

DATA itab2 TYPE itab.

FIELD-SYMBOLS <field> TYPE ANY.

READ TABLE itab1 INTO itab2 WITH KEY t_name = p_table1.

ASSIGN itab2-t_ref->* TO <tab>.

CREATE DATA t_ref1 LIKE LINE OF <tab>.

ASSIGN t_ref1->* TO <tab1>.

DO.

*READ TABLE <tab> ASSIGNING <tab1> INDEX 1.

READ TABLE <tab> ASSIGNING <tab1> INDEX SY-INDEX.

*WRITE:/ p_table1.

NEW-LINE.

IF sy-subrc <> 0.

EXIT.

ENDIF.

DO.

ASSIGN COMPONENT sy-index OF STRUCTURE <tab1> TO <field>.

IF sy-subrc <> 0.

EXIT.

ENDIF.

WRITE: <field>,' '.

ENDDO.

ENDDO.

ENDFORM. " print_table

Reward points if this Helps.

Manish

Read only

Former Member
0 Likes
814

You can use the syntax:

CREATE DATA dref { {TYPE tabkind OF [REF TO] {type|(name)}}

| {LIKE tabkind OF dobj} }

[WITH key]

[INITIAL SIZE n].

Example

Generation of an internal table for any database table and import of the first rows of that database table into the internal table. Since the data reference dref has a dynamic type, you have to use the field symbol to access the internal table. Field symbols <wa> and <comp> are required to dynamically access the individual rows and components.

PARAMETERS: dbtab(10) TYPE c,

rows TYPE i DEFAULT 100.

DATA dref TYPE REF TO data.

FIELD-SYMBOLS: TYPE ANY TABLE,

<wa> TYPE ANY,

TYPE ANY.

TRY.

CREATE DATA dref TYPE STANDARD TABLE OF (dbtab)

WITH NON-UNIQUE DEFAULT KEY.

ASSIGN dref->* TO .

SELECT *

FROM (dbtab) UP TO rows ROWS

INTO TABLE .

LOOP AT ASSIGNING <wa>.

DO.

ASSIGN COMPONENT sy-index

OF STRUCTURE <wa> TO .

IF sy-subrc = 0.

WRITE / .

ELSE.

EXIT.

ENDIF.

ENDDO.

ULINE.

ENDLOOP.

CATCH cx_sy_create_data_error.

WRITE 'Wrong Database!'.

ENDTRY.

-An excerpt from the documentation.

Regards,

ravi

Read only

jayanthi_jayaraman
Active Contributor
0 Likes
814

Hi,

Check this which I got from sdn.

<u><b>Method1:</b></u>

REPORT zpw_dyn_table.

TYPE-POOLS: slis.

PARAMETERS: p_tabnam LIKE dd02v-tabname .

DATA: ptr_itab TYPE REF TO data.

FIELD-SYMBOLS: <fs_itab> TYPE STANDARD TABLE. "ANY TABLE.

START-OF-SELECTION.

CREATE DATA ptr_itab TYPE STANDARD TABLE OF (p_tabnam).

ASSIGN ptr_itab->* TO <fs_itab>.

SELECT *

FROM (p_tabnam)

INTO TABLE <fs_itab> .

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

i_structure_name = p_tabnam

TABLES

t_outtab = <fs_itab>

EXCEPTIONS

program_error = 1

OTHERS = 2.

<u><b>Method2:</b></u>

FORM dynamic_code using p_tabname.

CLEAR itab. REFRESH itab.

APPEND 'PROGRAM SUBPOOL.' TO itab.

APPEND 'FORM UPLOAD.' TO itab.

APPEND 'DATA: BEGIN OF IT_TAB OCCURS 0.' TO itab.

CONCATENATE 'INCLUDE STRUCTURE'

p_tabname

'.' INTO wa_line SEPARATED BY space.

APPEND wa_line TO itab.

APPEND 'DATA: END OF IT_TAB.' TO itab.

APPEND 'DATA: BEGIN OF IT_TAB2 OCCURS 0.' TO itab.

CONCATENATE 'INCLUDE STRUCTURE'

p_tabname

'.' INTO w_line SEPARATED BY space.

APPEND w_line TO itab.

APPEND 'DATA: END OF IT_TAB2.' TO itab.

APPEND 'CALL FUNCTION ''WS_UPLOAD''' TO itab.

APPEND 'EXPORTING' TO itab.

CONCATENATE 'filename = ' ''''

c:\test '''' INTO wa_line SEPARATED BY space.

APPEND w_line TO itab.

APPEND 'filetype = ''DAT''' TO itab.

APPEND 'TABLES' TO itab.

APPEND 'DATA_TAB = IT_TAB.' TO itab.

APPEND 'ENDFORM.' TO itab.

GENERATE SUBROUTINE POOL itab NAME zdyn.

PERFORM upload IN PROGRAM (zdyn).

ENDFORM.

Read only

Former Member
0 Likes
815

Hi Deepak,

Plz refer the links below,

Regards,

Hema.

    • Reward points if it is useful.

Read only

Former Member
0 Likes
814

REPORT ZGILL_FS line-size 250 "use this code

line-count 65 .

parameter p_table type tabname.

field-symbols <tab> type table.

field-symbols <tab1> type any.

types: begin of itab,

t_name type tabname,

t_ref type ref to data,

end of itab.

data itab1 type table of itab with non-unique key t_name.

perform fetch_data using p_table.

perform print_table using p_table.

&----


*& Form fetch_data

&----


  • text

----


  • -->P_P_TABLE text

----


FORM fetch_data USING P_TABLE1 type tabname.

data itab2 type itab.

itab2-t_name = p_table1.

create data itab2-t_ref type table of (itab2-t_name) .

assign itab2-t_ref->* to <tab>.

append itab2 to itab1.

select * from (p_table1) up to 25 rows into corresponding fields of table <tab>.

ENDFORM. " fetch_data

&----


*& Form print_table

&----


  • text

----


  • -->P_P_TABLE text

----


FORM print_table USING P_TABLE1 type tabname.

DATA t_ref1 TYPE REF TO data.

DATA itab2 TYPE itab.

FIELD-SYMBOLS <field> TYPE ANY.

READ TABLE itab1 INTO itab2 WITH KEY t_name = p_table1.

ASSIGN itab2-t_ref->* TO <tab>.

CREATE DATA t_ref1 LIKE LINE OF <tab>.

ASSIGN t_ref1->* TO <tab1>.

DO.

*READ TABLE <tab> ASSIGNING <tab1> INDEX 1.

READ TABLE <tab> ASSIGNING <tab1> INDEX SY-INDEX.

*WRITE:/ p_table1.

NEW-LINE.

IF sy-subrc <> 0.

EXIT.

ENDIF.

DO.

ASSIGN COMPONENT sy-index OF STRUCTURE <tab1> TO <field>.

IF sy-subrc <> 0.

EXIT.

ENDIF.

WRITE: <field>,' '.

ENDDO.

ENDDO.

ENDFORM. " print_table

Read only

sourabhshah
Product and Topic Expert
Product and Topic Expert
0 Likes
814

hi,

This is a very simple method

Create a fieldcatalog with the desired fields and the call the method given below this creates a internal table at runtime.

DATA: lt_fieldcat TYPE lvc_t_fcat.

FIELD-SYMBOLS:<GT_TABLE1> TYPE TABLE.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING it_fieldcatalog = lt_fieldcat

IMPORTING ep_table = lp_table.

ASSIGN lp_table->* TO <gt_table1>.

Regards,

Sourabh