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: 

how to use dynamic internal table in interactive report syntax

aarif_baig
Active Participant
4 REPLIES 4

Former Member
0 Kudos

check below code....

DATA: l_val(3) TYPE n,

lf_mat(18) TYPE c.

  • Moving the Components-List details to the internal table 1st column

  • which will be used for creation of dynamic internal table

CLEAR gf_xfc.

gf_xfc-fieldname = text-t03.

gf_xfc-datatype = c_val_c.

gf_xfc-inttype = c_val_c.

gf_xfc-intlen = 18.

gf_xfc-decimals = 0.

APPEND gf_xfc TO gf_ifc.

CLEAR gf_xfc.

  • Moving the Description details to the internal table 2nd column

  • which will be used for creation of dynamic internal table

gf_xfc-fieldname = text-t04.

gf_xfc-datatype = c_val_c.

gf_xfc-inttype = c_val_c.

gf_xfc-intlen = 40.

gf_xfc-decimals = 0.

APPEND gf_xfc TO gf_ifc.

CLEAR gf_xfc.

  • Moving the Count details to the internal table 3rd column which will

  • be used for creation of dynamic internal table

gf_xfc-fieldname = text-t05.

gf_xfc-datatype = c_val_c.

gf_xfc-inttype = c_val_c.

gf_xfc-intlen = 5.

gf_xfc-decimals = 0.

APPEND gf_xfc TO gf_ifc.

  • Moving the Material numbers are moved to the internal table from 4th

  • column onwards till all the material numbers are moved to the columns

  • which will be used for creation of dynamic internal table. Here

  • columns will be reffered to as Material001 ..... Materialxxn

DO g_count TIMES.

CLEAR gf_xfc.

l_val = l_val + 1.

CONCATENATE text-t06 l_val INTO lf_mat.

gf_xfc-fieldname = lf_mat.

gf_xfc-datatype = c_val_c.

gf_xfc-inttype = c_val_c.

gf_xfc-intlen = 18.

gf_xfc-decimals = 0.

APPEND gf_xfc TO gf_ifc.

ENDDO.

  • Using the above data dynamic internal table is been created

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = gf_ifc

IMPORTING

ep_table = gf_table.

  • The dynamic internal table which is created is been assigned to

  • field-symbol which holds the data of the columns of X-axis

ASSIGN gf_table->* TO <fs_dyn_table>.

  • Dynamic work area is been created usng the reference to the

  • field-symbol which has the data and the line item is been assign to

  • field-symbol to hold line item data for reading purposes line by line

CREATE DATA gf_line LIKE LINE OF <fs_dyn_table>.

ASSIGN gf_line->* TO <fs_dyn_wa>.

if u need more info abt the code send an email to

rameshc@intelligroup.com

Former Member
0 Kudos

Hi Aarif,

Go through the below code for better understanding:

&----


*& Report ZDYN_INT_TABLE_02 *

*& *

&----


*& *

*& *

&----


REPORT ZDYN_INT_TABLE_02.

TYPE-POOLS: slis. " ALV Global types

SELECTION-SCREEN BEGIN OF BLOCK b1.

PARAMETERS : p_max(3) TYPE n OBLIGATORY.

SELECTION-SCREEN END OF BLOCK b1.

TYPES: BEGIN OF wa_gt_data,

matnr TYPE mara-matnr,

END OF wa_gt_data.

DATA gt_data TYPE STANDARD TABLE OF wa_gt_data WITH HEADER LINE.

----


*INITIALIZATION.

*

  • v_1 = 'Maximum of records to read'.

----


START-OF-SELECTION.

PERFORM f_read_data.

PERFORM f_display_data.

----


  • Form f_read_data

----


FORM f_read_data.

SELECT matnr INTO TABLE gt_data

FROM mara

UP TO p_max ROWS.

ENDFORM. " F_READ_DATA

----


  • Form F_DISPLAY_DATA

----


FORM f_display_data.

DATA:

l_column TYPE sy-tabix,

lp_struct TYPE REF TO data,

lp_table TYPE REF TO data, " Pointer to dynamic table

ls_lvc_cat TYPE lvc_s_fcat,

lt_lvc_cat TYPE lvc_t_fcat, " Field catalog

lt_fcat TYPE slis_t_fieldcat_alv, " Field catalog

ls_fieldcat TYPE slis_fieldcat_alv,

lt_fieldcat TYPE slis_t_fieldcat_alv, " Field catalog

ls_layout TYPE slis_layout_alv.

FIELD-SYMBOLS :

<header> TYPE ANY,

<field_header> TYPE ANY,

<field_mara> TYPE ANY,

<lt_data> TYPE table. " Data to display

ls_lvc_cat-fieldname = 'COLUMNTEXT'.

ls_lvc_cat-ref_table = 'LVC_S_DETA'.

APPEND ls_lvc_cat TO lt_lvc_cat.

ls_fieldcat-fieldname = 'COLUMNTEXT'.

ls_fieldcat-ref_tabname = 'LVC_S_DETA'.

ls_fieldcat-key = 'X'.

APPEND ls_fieldcat TO lt_fieldcat.

DO p_max TIMES.

  • For each line, a column 'VALUEx' is created in the fieldcatalog

  • Build Fieldcatalog

  • WRITE sy-index TO ls_lvc_cat-fieldname LEFT-JUSTIFIED.

CONCATENATE 'VALUE' ls_lvc_cat-fieldname

INTO ls_lvc_cat-fieldname.

ls_lvc_cat-ref_field = 'VALUE'.

ls_lvc_cat-ref_table = 'LVC_S_DETA'.

APPEND ls_lvc_cat TO lt_lvc_cat.

  • Build Fieldcatalog

CLEAR ls_fieldcat.

ls_fieldcat-fieldname = ls_lvc_cat-fieldname.

ls_fieldcat-ref_fieldname = 'VALUE'.

ls_fieldcat-ref_tabname = 'LVC_S_DETA'.

APPEND ls_fieldcat TO lt_fieldcat.

ENDDO.

  • Create internal table

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = lt_lvc_cat

IMPORTING

ep_table = lp_table.

ASSIGN lp_table->* TO <lt_data>.

  • Create structure = structure of the internal table

CREATE DATA lp_struct LIKE LINE OF <lt_data>.

ASSIGN lp_struct->* TO <header>.

  • Create field catalog from dictionary structure

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

i_structure_name = 'MARA'

CHANGING

ct_fieldcat = lt_fcat

EXCEPTIONS

inconsistent_interface = 1

program_error = 2

OTHERS = 3.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

LOOP AT lt_fcat INTO ls_fieldcat WHERE fieldname = 'MATNR'.

ASSIGN COMPONENT 1 OF STRUCTURE <header> TO <field_header>.

IF sy-subrc NE 0. EXIT .ENDIF.

READ TABLE lt_fcat INTO ls_fieldcat INDEX sy-index.

<field_header> = ls_fieldcat-seltext_m.

IF <field_header> IS INITIAL.

<field_header> = ls_fieldcat-fieldname.

ENDIF.

LOOP AT gt_data.

l_column = sy-tabix + 1.

ASSIGN COMPONENT 1 OF STRUCTURE gt_data TO <field_mara>.

IF sy-subrc NE 0. EXIT .ENDIF.

ASSIGN COMPONENT l_column OF STRUCTURE <header> TO <field_header>.

IF sy-subrc NE 0. EXIT .ENDIF.

WRITE <field_mara> TO <field_header> LEFT-JUSTIFIED.

ENDLOOP.

APPEND <header> TO <lt_data>.

ENDLOOP.

ls_layout-colwidth_optimize = 'X'.

ls_layout-no_colhead = 'X'.

ls_layout-zebra = 'X'.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

is_layout = ls_layout

it_fieldcat = lt_fieldcat

TABLES

t_outtab = <lt_data>.

ENDFORM. " F_DISPLAY_DATA

                        • END OF PROGRAM Z_ALV_LIST_TRANSPOSED *********************

*Always reward point for helpful answers

Regards,

Amit

Former Member
0 Kudos
"How to create a Dynamic Internal Table or Array? 
'Can you create an internal table dynamically? (at run time) 

'Yes , you can create a Dynamic Internal table .Just chek out this program . 
   

 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.

Reward points if it is usefull.......

Girish

Former Member
0 Kudos
"Another Example  :

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. 

Reward points if it is usefull ....

Girish