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 internaltable

Former Member
0 Likes
458

hi,

plz help me i want know about dynamic internal table.

3 REPLIES 3
Read only

Former Member
0 Likes
429

Veera,

Refer the below related threads

[]

[]

[http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb35de358411d1829f0000e829fbfe/content.htm]

[https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/2071] [original link is broken] [original link is broken] [original link is broken];

EX:

Dynamic internal table is internal table that we create on the fly with flexible column numbers.

For sample code, please look at this code tutorial. Hopefully it can help you

Check this link:

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

Sample code:

DATA: l_cnt(2) TYPE n,

l_cnt1(3) TYPE n,

l_nam(12),

l_con(18) TYPE c,

l_con1(18) TYPE c,

lf_mat TYPE matnr.

SORT it_bom_expl BY bom_comp bom_mat level.

CLEAR: l_cnt1, <fs_dyn_wa>.

Looping the component internal table

LOOP AT it_bom_expl INTO gf_it_bom_expl.

CLEAR: l_cnt1.

AT NEW bom_comp.

CLEAR: l_cnt, <fs_dyn_wa>, lf_mat.

For every new bom component the material data is moved to

temp material table which will be used for assigning the levels

checking the count

it_mat_temp] = it_mat[.

Component data is been assigned to the field symbol which is checked

against the field of dynamic internal table and the value of the

component number is been passed to the dynamic internal table field

value.

ASSIGN COMPONENT c_comp_list OF STRUCTURE <fs_dyn_wa> TO

<fs_check>.

<fs_check> = gf_it_bom_expl-bom_comp.

ENDAT.

AT NEW bom_mat.

CLEAR l_con.

ENDAT.

lf_mat = gf_it_bom_expl-bom_mat.

Looping the temp internal table and looping the dynamic internal table

*by reading line by line into workarea, the materialxxn is been assigned

to field symbol which will be checked and used.

LOOP AT it_mat_temp.

l_nam = c_mat.

l_cnt1 = l_cnt1 + 1.

CONCATENATE l_nam l_cnt1 INTO l_nam.

LOOP AT <fs_dyn_table2> ASSIGNING <fs_dyn_wa2>.

ASSIGN COMPONENT l_nam OF STRUCTURE <fs_dyn_wa2> TO <fs_xy>.

ENDLOOP.

IF <fs_xy> = lf_mat.

CLEAR lf_mat.

l_con1 = l_con.

ENDIF.

Checking whether the material exists for a component and if so it is

been assigned to the field symbol which is checked against the field

of dynamic internal table and the level of the component number

against material is been passed to the dynamic internal table field

value.

IF <fs_xy> = gf_it_bom_expl-bom_mat.

ASSIGN COMPONENT l_nam OF STRUCTURE <fs_dyn_wa> TO <fs_check>.

CLEAR l_con.

MOVE gf_it_bom_expl-level TO l_con.

CONCATENATE c_val_l l_con INTO l_con.

CONDENSE l_con NO-GAPS.

IF l_con1 NE space.

CONCATENATE l_con1 l_con INTO l_con SEPARATED BY c_comma.

CLEAR l_con1.

l_cnt = l_cnt - 1.

ENDIF.

<fs_check> = l_con.

l_cnt = l_cnt + 1.

ENDIF.

ENDLOOP.

AT END OF bom_comp.

At end of every new bom component the count is moved to the field

symbol which is checked against the field of dynamic internal table

and the count is been passed to the dynamic internal table field

value.

ASSIGN COMPONENT c_count OF STRUCTURE <fs_dyn_wa> TO <fs_check>.

<fs_check> = l_cnt.

INSERT <fs_dyn_wa> INTO TABLE <fs_dyn_table>.

ENDAT.

ENDLOOP.

Pls. reward if useful...

Read only

Former Member
0 Likes
429

Hi,

Generally if u declare a Internal Table u will pre-define the number of fields in the internal table.

IF u dont the number of fields and if u know the number of fields only at the Runtime ...

If may be any number such internal table is called as DYNAMIC internal Tables.

see this example code to create DYNAMIC INTERNAL TABLE and to display in in ALV.

REPORT z_alv_dynamic_data.
CONSTANTS :
  c_x VALUE 'X'.
*---------------------------------------------------------------------*
SELECTION-SCREEN :
BEGIN OF LINE, COMMENT 1(35) v_1 FOR FIELD p_table.         "#EC NEEDED
PARAMETERS p_table TYPE dd03l-tabname OBLIGATORY.
SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN :
BEGIN OF LINE, COMMENT 1(35) v_2 FOR FIELD p_max.           "#EC NEEDED
PARAMETERS p_max(2) TYPE n DEFAULT '20' OBLIGATORY.
SELECTION-SCREEN END OF LINE.
*---------------------------------------------------------------------*
AT SELECTION-SCREEN.

  PERFORM f_check_table.

*---------------------------------------------------------------------*
INITIALIZATION.

  v_1 = 'Table'.
  v_2 = 'Maximum of records'.

  GET PARAMETER ID 'DTB' FIELD p_table.
  IF p_table IS INITIAL.
    p_table = 'SPFLI'.
  ENDIF.

*---------------------------------------------------------------------*
START-OF-SELECTION.

  PERFORM f_display_data.

*---------------------------------------------------------------------*
*      Form  F_DISPLAY_DATA
*---------------------------------------------------------------------*
FORM f_display_data.

  TYPE-POOLS: slis.                    " ALV Global Types

* Macro definition
  DEFINE m_sort.
    add 1 to ls_sort-spos.
    ls_sort-fieldname = &1.
    ls_sort-up = c_x.
    append ls_sort to lt_sort.
  END-OF-DEFINITION.

  DATA:
    ls_dd03l    TYPE dd03l,            " Table Fields
    lt_dd03l    TYPE TABLE OF dd03l,   " Table Fields
    lp_struct   TYPE REF TO data,
    lp_table    TYPE REF TO data,      " Pointer to dynamic table
    of_sdescr   TYPE REF TO cl_abap_structdescr,
    ls_layout   TYPE slis_layout_alv,
    ls_lvc_cat  TYPE lvc_s_fcat,
    lt_lvc_cat  TYPE lvc_t_fcat,       " Field catalog
    ls_sort     TYPE slis_sortinfo_alv,
    lt_sort     TYPE slis_t_sortinfo_alv,  " Sort table
    ls_fieldcat TYPE slis_fieldcat_alv,
    lt_fieldcat TYPE slis_t_fieldcat_alv.  " Field catalog

  FIELD-SYMBOLS :
    <fieldcat>   TYPE slis_fieldcat_alv,
    <lt_data>    TYPE STANDARD TABLE,      " Data to display
    <fs>         TYPE ANY,
    <components> TYPE abap_compdescr.

* Dynamic creation of a structure
  CREATE DATA lp_struct TYPE (p_table).
  ASSIGN lp_struct->* TO <fs>.

* Fields Structure
  of_sdescr ?= cl_abap_typedescr=>describe_by_data( <fs> ).

  LOOP AT of_sdescr->components ASSIGNING <components>.
*   Build Fieldcatalog
    ls_lvc_cat-fieldname = <components>-name.
    ls_lvc_cat-ref_table = p_table.
    APPEND ls_lvc_cat TO lt_lvc_cat.
*   Build Fieldcatalog
    ls_fieldcat-fieldname = <components>-name.
    ls_fieldcat-ref_tabname = p_table.
    APPEND ls_fieldcat TO lt_fieldcat.
  ENDLOOP.

* Add checkbox
  CLEAR ls_lvc_cat.
  ls_lvc_cat-fieldname = 'CHECKBOX'.
  APPEND ls_lvc_cat TO lt_lvc_cat.

* 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>.

* Read data
  SELECT * FROM (p_table) UP TO p_max ROWS
    INTO CORRESPONDING FIELDS OF TABLE <lt_data>
   ORDER BY PRIMARY KEY.

  IF <lt_data> IS INITIAL.
*   No table entries found for specified key
    MESSAGE i429(mo).
    EXIT.
  ENDIF.

* Read key field to Build Sort Table
  SELECT * FROM dd03l INTO TABLE lt_dd03l
                     WHERE tabname  = p_table
                       AND fieldname <> '.INCLUDE'
                       AND as4vers  = '0000'
                       AND as4local = 'A'
                       AND keyflag = c_x.

  SORT lt_dd03l BY position.

* Build Sort Table
  LOOP AT lt_dd03l INTO ls_dd03l.
    READ TABLE lt_fieldcat ASSIGNING <fieldcat>
                            WITH KEY fieldname = ls_dd03l-fieldname.
    CHECK sy-subrc EQ 0.
*   Build Sort Table
    m_sort ls_dd03l-fieldname.
    <fieldcat>-key = c_x.
  ENDLOOP.

* Layout
  ls_layout-zebra = c_x.
  ls_layout-colwidth_optimize = c_x.
  ls_layout-group_change_edit = c_x.
  ls_layout-box_fieldname = 'CHECKBOX'.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      is_layout   = ls_layout
      it_fieldcat = lt_fieldcat
      it_sort     = lt_sort
    TABLES
      t_outtab    = <lt_data>.

ENDFORM.                               " F_DISPLAY_DATA
*---------------------------------------------------------------------*
*      Form  F_CHECK_TABLE
*---------------------------------------------------------------------*
FORM f_check_table .

  DATA :
    l_tabclass  TYPE tabclass,            " Table category
    l_viewclass TYPE viewclass.           " View Type

* Read table category
  SELECT SINGLE tabclass viewclass
           INTO (l_tabclass, l_viewclass)
           FROM dd02l
          WHERE tabname  = p_table
            AND as4local = 'A'
            AND as4vers  = '0000'.
  IF sy-subrc NE 0.
*   Table & is not active in the Dictionary
    MESSAGE e402(mo) WITH p_table.
  ELSEIF l_tabclass = 'INTTAB'.
*   & is a structure, not a table
    MESSAGE e403(mo) WITH p_table.
  ELSEIF l_tabclass = 'VIEW' AND l_viewclass NE 'D'.
*   Only use views of type "Maintenance view"
    MESSAGE e309(sv).
  ENDIF.

ENDFORM.                    " F_CHECK_TABLE
*************** END OF PROGRAM Z_ALV_DYNAMIC_DATA *********************

Regards,

Balakumar.G.

Reward points if helpful.

Read only

Former Member
0 Likes
429

Hi,

Following report is the sample report for Dynaimc alv report.

REPORT YSS_DYNAMIC_ALV.

TYPE-POOLS: SLIS.

FIELD-SYMBOLS: <DYN_TABLE> TYPE STANDARD TABLE,

<DYN_WA>.

DATA: ALV_FLDCAT TYPE SLIS_T_FIELDCAT_ALV,

IT_FLDCAT TYPE LVC_T_FCAT.

SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.

PARAMETERS: P_FLDS(5) TYPE C.

SELECTION-SCREEN END OF BLOCK B1.

START-OF-SELECTION.

build the dynamic internal table

PERFORM BUILD_DYN_ITAB.

write 5 records to the alv grid

DO 5 TIMES.

PERFORM BUILD_REPORT.

ENDDO.

call the alv grid.

PERFORM CALL_ALV.

************************************************************************

Build_dyn_itab

************************************************************************

FORM BUILD_DYN_ITAB.

DATA: NEW_TABLE TYPE REF TO DATA,

NEW_LINE TYPE REF TO DATA,

WA_IT_FLDCAT TYPE LVC_S_FCAT.

Create fields .

DO P_FLDS TIMES.

CLEAR WA_IT_FLDCAT.

WA_IT_FLDCAT-FIELDNAME = SY-INDEX.

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. "build_dyn_itab

*********************************************************************

Form build_report

*********************************************************************

FORM BUILD_REPORT.

DATA: FIELDNAME(20) TYPE C.

DATA: FIELDVALUE(5) TYPE C.

DATA: INDEX(3) TYPE C.

FIELD-SYMBOLS: <FS1>.

DO P_FLDS TIMES.

INDEX = SY-INDEX.

Set up fieldvalue

CONCATENATE 'FLD' INDEX INTO

FIELDVALUE.

CONDENSE FIELDVALUE NO-GAPS.

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

<fs1> = fieldvalue.

ENDDO.

Append to the dynamic internal table

APPEND <DYN_WA> TO <DYN_TABLE>.

ENDFORM. "build_report

************************************************************************

CALL_ALV

************************************************************************

FORM CALL_ALV.

DATA: WA_CAT LIKE LINE OF ALV_FLDCAT.

DO P_FLDS TIMES.

CLEAR WA_CAT.

WA_CAT-FIELDNAME = SY-INDEX.

WA_CAT-SELTEXT_S = SY-INDEX.

WA_CAT-OUTPUTLEN = '5'.

APPEND WA_CAT TO ALV_FLDCAT.

ENDDO.

Call ABAP List Viewer (ALV)

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

IT_FIELDCAT = ALV_FLDCAT

TABLES

T_OUTTAB = <DYN_TABLE>.

ENDFORM. "call_alv

Reward if helpful.