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
1,104

Hi All,

Can anyone tell me how to create a dynamic internal table? Any code for the dynamic internal table...

10 REPLIES 10
Read only

Former Member
0 Likes
1,045

check this

<a href="http://www.sap-img.com/ab030.htm">http://www.sap-img.com/ab030.htm</a>

regards

shiba dutta

Read only

Former Member
0 Likes
1,045

hi..

Check this sample.

REPORT ZV_DYNAMIC .

PARAMETERS: P_COL1 TYPE I,

P_COL2 TYPE I.

FIELD-SYMBOLS: <FS> TYPE ANY.

DATA: CHECK.

TYPES: BEGIN OF TYP_ITAB,

COL1 TYPE I,

COL2 TYPE I,

COL3 TYPE I,

COL4 TYPE I,

COL5 TYPE I,

COL6 TYPE I,

END OF TYP_ITAB.

DATA: ITAB TYPE TABLE OF TYP_ITAB, WA LIKE LINE OF ITAB.

WA-COL1 = 1.

WA-COL2 = 2.

WA-COL3 = 3.

WA-COL4 = 4.

WA-COL5 = 5.

WA-COL6 = 6.

APPEND WA TO ITAB.

WA-COL1 = 1.

WA-COL2 = 2.

WA-COL3 = 3.

WA-COL4 = 4.

WA-COL5 = 5.

WA-COL6 = 6.

APPEND WA TO ITAB.

DATA: COL1(20),

COL2(20),

CHAR,

CHAR2.

WRITE P_COL1 TO CHAR.

CONCATENATE 'COL' CHAR INTO COL1.

WRITE P_COL2 TO CHAR.

CONCATENATE 'COL' CHAR INTO COL2.

*FIELD-SYMBOLS <F> TYPE ANY.

*ASSIGN (COL1) TO <F>.

DATA: BEGIN OF ITAB1 OCCURS 0,

COL1 TYPE I,

COL2 TYPE I,

END OF ITAB1.

FIELD-SYMBOLS: <FS1> TYPE ANY,

<FS2> TYPE ANY.

DATA: VAR TYPE I.

LOOP AT ITAB ASSIGNING <FS>.

  • IF SY-TABIX = 1.

ASSIGN COMPONENT COL1 OF STRUCTURE <FS> TO <FS1>.

  • ELSE.

ASSIGN COMPONENT COL2 OF STRUCTURE <FS> TO <FS2>.

  • ENDIF.

  • APPEND <FS1> TO ITAB1.

MOVE <FS1> TO ITAB1-COL1.

MOVE <FS2> TO ITAB1-COL2.

APPEND ITAB1.

  • WRITE:/ <FS1>,<FS2>.

ENDLOOP.

LOOP AT ITAB1.

WRITE:/ ITAB1-COL1, ITAB1-COL2.

ENDLOOP.

<b>Reward points if useful</b>

Regards

Ashu

Read only

Former Member
0 Likes
1,045

HI,

see this program

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.

rgds,

bharat.

Read only

former_member194669
Active Contributor
0 Likes
1,045

Hi,

check this

data: dref type ref to data.

field-symbols: <wa> type any, <comp> type any.

create data dref type (query_table).

assign dref->* to <wa>.

https://www.sdn.sap.com/irj/sdn/wiki?path=/display/snippets/dynamicInternalTable&

aRs

Read only

Former Member
0 Likes
1,045

Dynamic Internal table

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

Read only

Former Member
0 Likes
1,045

<b>Field Symbols</b>

Field symbols are placeholders or symbolic names for other fields. They do not physically reserve space for a field, but point to its contents. A field symbol cam point to any data object. The data object to which a field symbol points is assigned to it after it has been declared in the program.

Whenever you address a field symbol in a program, you are addressing the field that is assigned to the field symbol. After successful assignment, there is no difference in ABAP whether you reference the field symbol or the field itself. You must assign a field to each field symbol before you can address the latter in programs.

Example using the obsolete STRUCTURE addition:

DATA: wa(10) VALUE '0123456789'.

DATA: BEGIN OF line1,
         col1(3),
         col2(2),
         col3(5),
      END OF line1.

DATA: BEGIN OF line2,
         col1(2),
         col2 LIKE sy-datum,
      END OF line2.

FIELD-SYMBOLS: <f1> STRUCTURE line1 DEFAULT wa,
               <f2> STRUCTURE line2 DEFAULT wa.

WRITE: / <f1>-col1, <f1>-col2, <f1>-col3,
       / <f2>-col1, <f2>-col2.

Example using the correct syntax (TYPE and CASTING):

DATA: wa(10) VALUE '0123456789'.

DATA: BEGIN OF line1,
         col1(3),
         col2(2),
         col3(5),
      END OF line1.

DATA: BEGIN OF line2,
         COL1(2),
         COL2 LIKE sy-datum,
      END OF line2.

FIELD-SYMBOLS: <f1> LIKE line1.
ASSIGN wa TO <f1> CASTING.

FIELD-SYMBOLS: <f2> LIKE line2.
ASSIGN wa TO <f2> CASTING.

WRITE: / <f1>-col1, <F1>-col2, <F1>-col3,
       / <f2>-col1, <F2>-col2.

In both cases, the list appears as follows:

012 34 56789
01 2345/67/89

reward points if it is usefull.....

Girish

Read only

Former Member
0 Likes
1,045

Hi,

Dynamic internal tables can be created using CL_ALV_TABLE_CREATE class and method CREATE_DYNAMIC_TABLE.

Just fill the field catalog(IT_FIELDCATALOG) in the parameter of method (like you do in normal ALV reports) and it creates a pointer to dynamic data table.

Check this code.

report ytest.

data: lt_fieldcatalog type lvc_t_fcat.

data: ls_fieldcatalog type lvc_s_fcat.

field-symbols: <fs_data> type ref to data.

field-symbols: <fs_1>.

field-symbols: <fs_2> type any table.

field-symbols: <fs_3> type ypoll.

data: lt_data type ref to data.

assign lt_data to <fs_data>.

ls_fieldcatalog-fieldname = 'MANDT'.

ls_fieldcatalog-tabname = 'LT_TAB'.

append ls_fieldcatalog to lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'POLLID'.

ls_fieldcatalog-tabname = 'LT_TAB'.

append ls_fieldcatalog to lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'TEAM'.

ls_fieldcatalog-tabname = 'LT_TAB'.

append ls_fieldcatalog to lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'INITIATOR'.

ls_fieldcatalog-tabname = 'LT_TAB'.

append ls_fieldcatalog to lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'DESCRIPTION'.

ls_fieldcatalog-tabname = 'LT_TAB'.

append ls_fieldcatalog to lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'APPROVED'.

ls_fieldcatalog-tabname = 'LT_TAB'.

append ls_fieldcatalog to lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'INITIATED_DATE'.

ls_fieldcatalog-tabname = 'LT_TAB'.

append ls_fieldcatalog to lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'END_DATE'.

ls_fieldcatalog-tabname = 'LT_TAB'.

append ls_fieldcatalog to lt_fieldcatalog.

ls_fieldcatalog-fieldname = 'WINNER'.

ls_fieldcatalog-tabname = 'LT_TAB'.

append ls_fieldcatalog to lt_fieldcatalog.

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = lt_fieldcatalog

importing

ep_table = <fs_data>

exceptions

generate_subpool_dir_full = 1

others = 2

.

if sy-subrc <> 0.

endif.

assign <fs_data>->* to <fs_1>.

assign <fs_1> to <fs_2>.

loop at <fs_2> assigning <fs_3>.

write: <fs_3>-pollid.

endloop.

<b>Reward points if this helps,</b>

Kiran

Read only

Former Member
0 Likes
1,045

Hi,

go through the following example----


*Data definitions

      • Tables

DATA: LT_DATA type ref to DATA.

DATA: LT_FIELDCATALOG type LVC_T_FCAT.

      • Structure

DATA: LS_FIELDCATALOG type LVC_S_FCAT.

      • Data References

DATA: NEW_LINE type ref to data,

FS_DATA type ref to data.

      • Field Symbols

FIELD-SYMBOLS: <FS_DATA> type ref to DATA,

<FS_1> type any table,

<FS_2>,

<FS_3>.

*Populating the internal table with fieldnames required for our dynamic

*internal table

LS_FIELDCATALOG-FIELDNAME = 'MANDT'.

append LS_FIELDCATALOG to LT_FIELDCATALOG.

LS_FIELDCATALOG-FIELDNAME = 'CARRID'. "Fieldname

LS_FIELDCATALOG-INTTYPE = 'C'. "Internal Type C-> Character

append LS_FIELDCATALOG to LT_FIELDCATALOG.

LS_FIELDCATALOG-FIELDNAME = 'CONNID'.

LS_FIELDCATALOG-INTTYPE = 'N'.

append LS_FIELDCATALOG to LT_FIELDCATALOG.

LS_FIELDCATALOG-FIELDNAME = 'FLDATE'.

LS_FIELDCATALOG-INTTYPE = 'D'.

append LS_FIELDCATALOG to LT_FIELDCATALOG.

LS_FIELDCATALOG-FIELDNAME = 'PRICE'.

LS_FIELDCATALOG-INTTYPE = 'P'.

append LS_FIELDCATALOG to LT_FIELDCATALOG.

LS_FIELDCATALOG-FIELDNAME = 'CURRENCY'.

LS_FIELDCATALOG-INTTYPE = 'C'.

append LS_FIELDCATALOG to LT_FIELDCATALOG.

*Calling the method CREATE_DYNAMIC_TABLE

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = LT_FIELDCATALOG

importing

ep_table = FS_DATA

exceptions

generate_subpool_dir_full = 1

others = 2

.

if sy-subrc <> 0.

endif.

*Assigning Field-Symbol to our dynamic internal table

assign LT_DATA to <FS_DATA>.

*Internal Table is ready, now to put data in that table

      • So <FS_1> now points to our dynamic internal table.

assign FS_DATA->* to <FS_1>.

      • Next step is to create a work area for our dynamic internal table.

create data NEW_LINE like line of <FS_1>.

      • A field-symbol to access that work area

assign NEW_LINE->* to <FS_2>.

      • And to put the data in the internal table

select

MANDT

CARRID

CONNID

FLDATE

PRICE

CURRENCY

from SFLIGHT

into corresponding fields of table <FS_1>.

      • Access contents of internal table

loop at <FS_1> assigning <FS_2>.

do 5 times.

assign component sy-index of structure <FS_2> to <FS_3>.

write: <FS_3>.

enddo.

skip 1.

endloop.

TOP-OF-PAGE.

WRITE:/5 'FUJITSU CONSULTING COMPANY' INVERSE COLOR 6,

50 SY-DATUM INVERSE COLOR 6,

70 SY-PAGNO INVERSE COLOR 6.

ULINE.

****do rewards if usefull

vijay

Read only

Former Member
0 Likes
1,045

hi,

chk the below code and link.

very hlpful

REPORT zdynarjtry.

PARAMETERS: comp(80).

DATA: dref TYPE REF TO data.

FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE.

CREATE DATA dref TYPE TABLE OF (comp).

ASSIGN dref->* TO <dyn_table>.

SELECT * FROM (comp)

INTO TABLE <dyn_table>.

BREAK-POINT.

See the below links for more programs:-

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

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

<b>Reward points</b>

Regards

Read only

Former Member
0 Likes
1,045

Hi venkatesh,

1.

For this purpose,

in my program,

there is an INDEPENDENT FORM

whose inputs are

LIST OF FIELDS, (just as u require)

and from those, it consructs dynamic table.

2. Here is the program.

the dynamic table name will be

<DYNTABLE>.

3. U can use this program (FORM in this program)

to generate any kind of internal table

by specifying list of fields.

4.

REPORT abc.

*----


COMPULSORY

FIELD-SYMBOLS: <dyntable> TYPE ANY TABLE.

FIELD-SYMBOLS: <dynline> TYPE ANY.

DATA: lt TYPE lvc_t_fcat.

DATA: ls TYPE lvc_s_fcat.

FIELD-SYMBOLS: <fld> TYPE ANY.

DATA : fldname(50) TYPE c.

DATA : ddfields LIKE ddfield OCCURS 0 WITH HEADER LINE.

*----


START-OF-SELECTION.

*----


field list

ddfields-fieldname = 'BUKRS'.

APPEND DDFIELDS.

ddfields-fieldname = 'MATNR'.

APPEND DDFIELDS.

*----


PERFORM mydyntable .

*----


see <DYNTABLE> in debug mode.

BREAK-POINT.

*----


  • INDEPENDENT FORM

*----


FORM mydyntable .

*----


Create Dyn Table From FC

FIELD-SYMBOLS: <fs_data> TYPE REF TO data.

FIELD-SYMBOLS: <fs_1>.

FIELD-SYMBOLS: <fs_2> TYPE ANY TABLE.

DATA: lt_data TYPE REF TO data.

data : lt TYPE lvc_t_fcat .

*----


CONSTRUCT FIELD LIST

LOOP AT ddfields.

ls-fieldname = ddfields-fieldname.

APPEND ls TO lt.

ENDLOOP.

ASSIGN lt_data TO <fs_data>.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = lt

IMPORTING

ep_table = <fs_data>

EXCEPTIONS

generate_subpool_dir_full = 1

OTHERS = 2.

IF sy-subrc <> 0.

ENDIF.

*----


Assign Dyn Table To Field Sumbol

ASSIGN <fs_data>->* TO <fs_1>.

ASSIGN <fs_1> TO <fs_2>.

ASSIGN <fs_1> TO <dyntable>.

ENDFORM. "MYDYNTABLE

regards,

amit m.