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 TABLES

Former Member
0 Kudos
1,942

WHAT R DYNAMICINTERNAL TABLES?

HOW CAN WE CREATE IT ?HAT R ITS ADBVANTAGES?

1 ACCEPTED SOLUTION

Former Member
0 Kudos
668

Dynamic Internal Tables are tables created at RUN TIME Using Field Symbols.

It's useful in creating a program where you don't know name of table till run time. In other words it's very useful in creating Dynamic Programs.

Hope this will answer your query.

Darshan.

8 REPLIES 8

Former Member
0 Kudos
669

Dynamic Internal Tables are tables created at RUN TIME Using Field Symbols.

It's useful in creating a program where you don't know name of table till run time. In other words it's very useful in creating Dynamic Programs.

Hope this will answer your query.

Darshan.

Former Member
0 Kudos
668
dynamic internal table is a table which is not defined until runtime, inside ABAP code.
Dynamic internal table is internal table that we create on the fly with flexible column numbers. 

Sample OCde: 


 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.

Former Member
0 Kudos
668

we use Dynamic internal tables when we use ABAP oops

we create them by using Field symbols

There are a few declarations to make:

field-symbols: <table> type any.

types: fieldref type ref to data.

data: dyn_table type fieldref.

As for the actual code to generate the table:

create data dyn_table type (SAP Table).

assign dyn_table->* to table.

The table is now a field symbol which can be referenced in the normal way.

check out this prg

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.

for more info visit

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

regards

Giridhar

sharat_chandra
Advisor
Advisor
0 Kudos
668

Hi,

Please have a look at the method create_dynamic_table of class cl_alv_table_create. Here you can to determine the line type of the internal table dynamically by constructing a field catalog with the dynamically defined fields.

Also you can use the Run time type services RTTS to create strcutures , tables

etc dynamically in programs. The class CL_ABAPTABLEDESCR is used to create dynamic tables in the programs.

Regards,

Sharat

uwe_schieferstein
Active Contributor
0 Kudos
668

Hello Tanuj

Have a look at sample report <b>ZUS_SDN_RTTI_CREATE_STRUCTUR_2</b> in thread

The discussion in this thread is quite interesting and may answer your question.

Regards

Uwe

Former Member
0 Kudos
668

Former Member
0 Kudos
668

Hi,

DATA: lw_fieldvalue(40) TYPE c,

lw_fieldname(30) TYPE c.

gwa_output TYPE REF TO data.

FIELD-SYMBOLS: <gwa_output> TYPE ANY.

FIELD-SYMBOLS: <l_fvalue> TYPE ANY.

*---Create a pointer to store the workarea of the final output table.

CREATE DATA gwa_output LIKE LINE OF <git_output>.(this is the output table)

*---Assign it to a fieldsymbol which will be used in populating the

*---final output table.

ASSIGN gwa_output->* TO <gwa_output>.

Loop at i_tab into wa_tab.

move wa_tab-field1 to v_fieldvalue. (moving the first field value into field value variable).

CONDENSE v_fieldvalue NO-GAPS. (Condense the output).

CLEAR: <gwa_output>.

ASSIGN COMPONENT 'FIELD1' OF STRUCTURE <gwa_output> TO <l_fvalue>.

<l_fvalue> = V_fieldvalue.

Repeat the same with other fields

*---Append the current record to final output table.

APPEND <gwa_output> TO <git_output>.

endloop.

You need to assign the component of table <dyn_Tab> to field symbol and than give value to that field symbol.

Like:

ASSIGN COMPONENT FLD1 OF STRUCTURE <DYN_TAB> TO <FS>.

<FS> = I_TAB-FLD1.

Try this code, it generates field names dynamically

  • 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-field2 = ''19970814''.'. APPEND inctabl.

inctabl-line = 'dyntab-field3 = 1.'. APPEND inctabl.

inctabl-line = 'append dyntab.'. APPEND inctabl.

inctabl-line = ' '. APPEND inctabl.

inctabl-line = 'loop at dyntab.'. APPEND inctabl.

inctabl-line = 'write: / dyntab-field1, dyntab-field2, dyntab-field3.'.

APPEND inctabl.

inctabl-line = 'endloop.'. APPEND inctabl.

  • Create and run the dynamic program

INSERT REPORT 'ZDYNPRO' FROM inctabl.

IF sy-subrc = 0.

SUBMIT zdynpro.

ELSE.

WRITE : / 'error'.

ENDIF.

Check this link:

http://www.saptechnical.com/Tutorials/ABAP/DynamicInternaltable/DynamicInternalTable.htm

Former Member
0 Kudos
668

Hi,

Check this post which has been posted today

Regards,

Satish