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 tables

Former Member
0 Likes
949

Hi!

I have problem in understanding the flow of how to

create a dynamic internal table.This is what i have understood.

1> A dynamic internal table is created by creating a new

include file at run time and having it inserted in the current program.

2> This include file will contain the structure of the internal table, which is to be created at run time.

3>But when and where do i populate this table?

Can you help me out with this problem?

6 REPLIES 6
Read only

Former Member
0 Likes
786

Hi abhijeet,

1. Your points 1,2 are incorrect.

2. Dynanmic internal table has nothing

to do with include file etc.

3. It has to do with field-symbols.

4. If your requirement is

creating a dynamic internal table,

(provided u have the information

about which-which fields should be in the table)

, then u can use the below code

5.

-


There is an INDEPENDENT FORM

whose inputs are FIELD LIST

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 some inputs (ie. field list)

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.

*----


PARAMETERS : infty(4) TYPE c OBLIGATORY.

DATA : iname LIKE dd02l-tabname.

START-OF-SELECTION.

*----


GET INFO

CONCATENATE 'P' infty INTO iname.

DATA : ddfields LIKE ddfield OCCURS 0 WITH HEADER LINE.

CALL FUNCTION 'DD_NAMETAB_TO_DDFIELDS'

EXPORTING

tabname = iname

TABLES

ddfields = ddfields.

.

*----


CONSTRUCT FIELD LIST

LOOP AT ddfields.

ls-fieldname = ddfields-fieldname.

APPEND ls TO lt.

ENDLOOP.

*----


PERFORM

PERFORM mydyntable USING lt.

BREAK-POINT.

*----


  • INDEPENDENT FORM

*----


FORM mydyntable USING lt TYPE lvc_t_fcat .

*----


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.

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.

Read only

jayanthi_jayaraman
Active Contributor
0 Likes
786

Hi,

Check this sample code.It will answer your doubts and kindly reward points by clikcing the star on the left of reply,if it helps.This code I got from SDN.

report zrich_0003 no standard page heading.

type-pools: slis.

field-symbols: <dyn_table> type standard table,

data: alv_fldcat type slis_t_fieldcat_alv,

it_fldcat type lvc_t_fcat.

<dyn_wa>.

selection-screen begin of block b1 with frame title text-001.

parameters: p_check type c.

selection-screen end of block b1.

start-of-selection.

perform build_dyn_itab.

perform build_report.

loop at <dyn_table> into <dyn_wa>.

write:/ <dyn_wa>.

endloop.

Build_dyn_itab************************************************************************

form build_dyn_itab.

data: index(3) type c.

data: new_table type ref to data,

new_line type ref to data,

wa_it_fldcat type lvc_s_fcat.

clear wa_it_fldcat.

wa_it_fldcat-fieldname = 'AUFNR'.

wa_it_fldcat-datatype = 'CHAR'.

wa_it_fldcat-intlen = 12.

append wa_it_fldcat to it_fldcat .

clear wa_it_fldcat.

wa_it_fldcat-fieldname = 'POSNR'.

wa_it_fldcat-datatype = 'CHAR'.

wa_it_fldcat-intlen = 6.

append wa_it_fldcat to it_fldcat .

  • Create fields

clear index.

do 2 times.

index = sy-index.

clear wa_it_fldcat.

concatenate 'Field' index into

wa_it_fldcat-fieldname .

condense wa_it_fldcat-fieldname no-gaps.

wa_it_fldcat-datatype = 'CHAR'.

wa_it_fldcat-intlen = 5.

append wa_it_fldcat to it_fldcat .

enddo.

  • Create dynamic internal table and assign to FS

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = it_fldcat

importing

ep_table = new_table. assign new_table->* to <dyn_table>.

  • Create dynamic work area and assign to FS

create data new_line like line of <dyn_table>.

assign new_line->* to <dyn_wa>.

endform.

Form build_report*********************************************************************

form build_report.

data: fieldname(20) type c.

data: fieldvalue(5) type c.

data: index(3) type c.

field-symbols: <fs1>.

assign component 'AUFNR' of structure <dyn_wa> to <fs1>.

<fs1> = '123456789'.

assign component 'POSNR' of structure <dyn_wa> to <fs1>.

<fs1> = '000001'.

do 2 times.

index = sy-index.

  • Set up fieldname

concatenate 'FIELD' index into fieldname .

condense fieldname no-gaps.

  • Set up fieldvalue

concatenate 'FLD' index into fieldvalue.

condense fieldvalue no-gaps.

assign component fieldname of structure <dyn_wa> to <fs1>.

<fs1> = fieldvalue.

enddo.

  • Append to the dynamic internal table

append <dyn_wa> to <dyn_table>.

endform.

Read only

Former Member
0 Likes
786

Have a look at these threads from the forum:

Hope this helps.

Sudha

Read only

vinod_gunaware2
Active Contributor
0 Likes
786

hi

Below two example resolve ur query. Just check it out.

REPORT ZCLUST1 .

  • Example: how to create a dynamic internal table

  • The dynamic internal table stucture

DATA: BEGIN OF STRUCT OCCURS 10,

FILDNAME(8) TYPE C,

ABPTYPE TYPE C,

LENGTH TYPE I,

END OF STRUCT.

  • The dynamic program source table

DATA: BEGIN OF INCTABL OCCURS 10,

LINE(72),

END OF INCTABL.

DATA: LNG TYPE I, TYPESRTING(6).

  • Sample dynamic internal table stucture

STRUCT-FILDNAME = 'field1'. STRUCT-ABPTYPE = 'c'. STRUCT-LENGTH = '6'.

APPEND STRUCT. CLEAR STRUCT.

STRUCT-FILDNAME = 'field2'. STRUCT-ABPTYPE = 'd'.

APPEND STRUCT. CLEAR STRUCT.

STRUCT-FILDNAME = 'field3'. STRUCT-ABPTYPE = 'i'.

APPEND STRUCT. CLEAR STRUCT.

  • Create the dynamic internal table definition in the dyn. program

INCTABL-LINE = 'program zdynpro.'. APPEND INCTABL.

INCTABL-LINE = 'data: begin of dyntab occurs 10,'. APPEND INCTABL.

LOOP AT STRUCT.

INCTABL-LINE = STRUCT-FILDNAME.

LNG = STRLEN( STRUCT-FILDNAME ).

IF NOT STRUCT-LENGTH IS INITIAL .

TYPESRTING(1) = '('.

TYPESRTING+1 = STRUCT-LENGTH.

TYPESRTING+5 = ')'.

CONDENSE TYPESRTING NO-GAPS.

INCTABL-LINE+LNG = TYPESRTING.

ENDIF.

INCTABL-LINE+15 = 'type '.

INCTABL-LINE+21 = STRUCT-ABPTYPE.

INCTABL-LINE+22 = ','.

APPEND INCTABL.

ENDLOOP.

INCTABL-LINE = 'end of dyntab. '.

APPEND INCTABL.

  • Create the code processes the dynamic internal table

INCTABL-LINE = ' '. APPEND INCTABL.

INCTABL-LINE = 'dyntab-field1 = ''aaaaaa''.'. APPEND INCTABL.

INCTABL-LINE = 'dyntab-field1 = ''19970814''.'. APPEND INCTABL.

INCTABL-LINE = 'dyntab-field1 = 1.'. APPEND INCTABL.

INCTABL-LINE = 'append dyntab.'. APPEND INCTABL.

INCTABL-LINE = ' '. APPEND INCTABL.

INCTABL-LINE = 'loop at dyntab.'. APPEND INCTABL.

INCTABL-LINE = 'write: / dyntab.'. APPEND INCTABL.

INCTABL-LINE = 'endloop.'. APPEND INCTABL.

  • Create and run the dynamic program

INSERT REPORT 'zdynpro'(001) FROM INCTABL.

SUBMIT ZDYNPRO.

-


or Just try out this simpler dynamic internal tables

DATA: itab TYPE STANDARD TABLE OF spfli,

wa LIKE LINE OF itab.

DATA: line(72) TYPE c,

list LIKE TABLE OF line(72).

START-OF-SELECTION.

*line = ' CITYFROM CITYTO '.

line = ' AIRPTO '.

APPEND line TO list.

SELECT DISTINCT (list)

INTO CORRESPONDING FIELDS OF TABLE itab

FROM spfli.

IF sy-subrc EQ 0.

LOOP AT itab INTO wa.

  • WRITE: / wa-cityfrom, wa-cityto.

WRITE 😕 wa-airpto.

ENDLOOP.

ENDIF.

regards

vinod

Read only

hymavathi_oruganti
Active Contributor
0 Likes
786

1. y u r usig dynamic internal table?

suppose there is a requirement like below.

i have an internal table, i donno how many values it populates.

<b>suppose , A, B, C are the fields of itab.</b>

now my requirement is the <b>values of C</b> should be stored <b>in another internal table</b>.

how many values i will get in C?

not known prior.

<b>then how can i declare an internal table with fields as values of c?</b>

then we need an internal table which is not declared above statically with fields known before but a dynamic table which is <b>built automatically with fields that came at run time.</b>

i hope now u understood the main concept of dynamic internal table.

example:

suppose itab has fields A, B, C.

SO ITAB-C HAS THE VALUES OF C. NOW WE WANT A TABLE WITH VALUES OF C RIGHT!!.

NOW BUILD A FIELDCATALOG LIKE BELOW.

<b>

LOOP AT ITAB.

LS_FIELDCAT-FIELDNAME = ITAB-C.

LS_FIELDCAT-COL_TEXT = ITAB_C.

APPEND LS_FIELDCAT TO LT_FIELDCAT.

ENDLOOP.

DATA: DREF TYPE REF TO DATA.

FIELD-SYMBOLS: <TEMP_TAB> TYPE ANY.

CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE

EXPORTING

IT_FIELDCATALOG = LT_LVCFIELDCAT

IMPORTING

EP_TABLE = DREF.

ASSIGN dref->* TO <TEMP_TAB>.</b>

<TEMP_TAB> IS THE REQUIRED DYNAMIC INTERNAL TABLE.

NOW IF U WANT TO FILL THE DYNAMIC INTERNAL TABLE.

DO AS BELOW..

<b>DATA: WA_DREF TYPE REF TO DATA.

FIELD-SYMBOLS: <WA_TAB> TYPE ANY.

CREATE DATA WA_DREF LIKE LINE OF <TEMP_TAB>.

ASSIGN WA_DREF->* TO <WA_TAB>.

FIELD-SYMBOLS: <FS1>, <FS2>.

LOOP AT ITAB.

ASSIGN COMPONENT ITAB-C OF STRUCTURE <WA_TAB> TO <FS1>.

<FS1> = {SOME VALUE}.

APPEND <WA_TAB> TO <TEMP_TAB>.

ENDLOOP.

</b>

Message was edited by: Hymavathi Oruganti

Read only

Former Member
0 Likes
786

Hi,

Refer this sample code for creating an dynamic Internal Table.


 
REPORT ZCLUST1 .
*
* Example: how to create a dynamic internal table
* 
* The dynamic internal table stucture
DATA: BEGIN OF STRUCT OCCURS 10,
    FILDNAME(8) TYPE C,
    ABPTYPE TYPE C,
    LENGTH TYPE I,
END OF STRUCT.

* The dynamic program source table
DATA: BEGIN OF INCTABL OCCURS 10,
    LINE(72),
END OF INCTABL.

DATA: LNG TYPE I, TYPESRTING(6).

* Sample dynamic internal table stucture
STRUCT-FILDNAME = 'field1'. STRUCT-ABPTYPE = 'c'. STRUCT-LENGTH = '6'.
APPEND STRUCT. CLEAR STRUCT.
STRUCT-FILDNAME = 'field2'. STRUCT-ABPTYPE = 'd'.
APPEND STRUCT. CLEAR STRUCT.
STRUCT-FILDNAME = 'field3'. STRUCT-ABPTYPE = 'i'.
APPEND STRUCT. CLEAR STRUCT.

* Create the dynamic internal table definition in the dyn. program
INCTABL-LINE = 'program zdynpro.'. APPEND INCTABL.
INCTABL-LINE = 'data: begin of dyntab occurs 10,'. APPEND INCTABL.
LOOP AT STRUCT.
  INCTABL-LINE = STRUCT-FILDNAME.
  LNG = STRLEN( STRUCT-FILDNAME ).
  IF NOT STRUCT-LENGTH IS INITIAL .
    TYPESRTING(1) = '('.
    TYPESRTING+1 = STRUCT-LENGTH.
    TYPESRTING+5 = ')'.
    CONDENSE TYPESRTING NO-GAPS.
    INCTABL-LINE+LNG = TYPESRTING.
  ENDIF.
  INCTABL-LINE+15 = 'type '.
  INCTABL-LINE+21 = STRUCT-ABPTYPE.
  INCTABL-LINE+22 = ','.
  APPEND INCTABL.
ENDLOOP.
INCTABL-LINE = 'end of dyntab. '.
APPEND INCTABL.

* Create the code processes the dynamic internal table
INCTABL-LINE = ' '. APPEND INCTABL.
INCTABL-LINE = 'dyntab-field1 = ''aaaaaa''.'. APPEND INCTABL.
INCTABL-LINE = 'dyntab-field1 = ''19970814''.'. APPEND INCTABL.
INCTABL-LINE = 'dyntab-field1 = 1.'. APPEND INCTABL.
INCTABL-LINE = 'append dyntab.'. APPEND INCTABL.
INCTABL-LINE = ' '. APPEND INCTABL.
INCTABL-LINE = 'loop at dyntab.'. APPEND INCTABL.
INCTABL-LINE = 'write: / dyntab.'. APPEND INCTABL.
INCTABL-LINE = 'endloop.'. APPEND INCTABL.

* Create and run the dynamic program
INSERT REPORT 'zdynpro'(001) FROM INCTABL.
SUBMIT ZDYNPRO.

Regards,

Gayathri