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
561

Hi all,

I want to create dynamic internal tables where 1st 3 colums are fixed but then other colums have to be added at runtime, depending upon the requirement. Please help solve this issue.

Thanx in advance

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
517

Hi,

Check out this blog!

/people/rich.heilman2/blog/2005/07/27/dynamic-internal-tables-and-structures--abap

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

As it will help you ... don't forget the reward!

5 REPLIES 5
Read only

Former Member
0 Likes
518

Hi,

Check out this blog!

/people/rich.heilman2/blog/2005/07/27/dynamic-internal-tables-and-structures--abap

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

As it will help you ... don't forget the reward!

Read only

0 Likes
517

Please check out this sample program.



report zrich_0003
       no standard page heading.

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

* Create fields
  clear index.
  do 10 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>.

  do 10 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.

Regards,

Rich Heilman

Read only

0 Likes
517

Hi Rich

I have a requirment where no of columns in report is not "fixed" .

For example imagine an internal table where 4 firxed colums and non fixed variable columns 

Example

First Row will have 4 fixed columns and variable 20 columns

Second row will have 4 fixed columns and variable 25 colums

Third row will have 4 fixed columns and variable 40 columns.

Variable columns can be 'n' number.( not fixed )

Is it possible to achieve the requirment using "dynamic internal table" .

Regards

Avi

Read only

0 Likes
517

All rows of the same table have the same number of columns if you need to create rows with different number of columns or even columns that differ from each row you need to create multiples internal tables

Read only

vamsilakshman_pendurti
Active Participant
0 Likes
517

Hi

Just observe this program ..

i thing it is useful to u.........

*&---------------------------------------------------------------------*
*& Report 
YVAMC_DYN_TABLE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  YVAMC_DYN_TABLE.
TYPE-POOLS SLIS .
TYPES: BEGIN OF TY_VBAK,
         KUNNR TYPE KUNNR,
         VBELN TYPE VBELN,
         ERNAM TYPE ERNAM,
       END OF TY_VBAK.


DATA: T_VBAK TYPE TABLE OF TY_VBAK,
        W_VBAK TYPE TY_VBAK,
        W_FCAT TYPE LVC_S_FCAT,
        T_FCAT TYPE LVC_T_FCAT,
        G_KUNNR TYPE KNA1-KUNNR,
        DY_TABLE TYPE REF TO DATA ,
        IT_FCAT TYPE  SLIS_T_FIELDCAT_ALV .

FIELD-SYMBOLS: <DY_TABLE> TYPE  STANDARD TABLE,
                 <DY_WA> TYPE ANY,
                 <FIELD> TYPE ANY.

SELECT-OPTIONS S_KUNNR FOR  G_KUNNR NO INTERVALS.

START-OF-SELECTION.

   PERFORM GET_VBAK.
   PERFORM GET_FCAT.
   PERFORM POPULATE_DYN.
*&---------------------------------------------------------------------*
*&      Form  GET_VBAK
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM GET_VBAK .
   SELECT KUNNR
           VBELN
          ERNAM

          FROM VBAK
          INTO TABLE T_VBAK
         WHERE KUNNR IN S_KUNNR.
   IF SY-SUBRC <> 0.
     MESSAGE 'No Data' TYPE 'I'.
     LEAVE LIST-PROCESSING.
   ENDIF.
ENDFORM.                    " GET_VBAK
*&---------------------------------------------------------------------*
*&      Form  GET_FCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM GET_FCAT .
   W_FCAT-FIELDNAME = 'KUNNR'.
   W_FCAT-SCRTEXT_L = 'Customer'.
*  W_FCAT-COLTEXT = 'Customer'.
   APPEND W_FCAT TO T_FCAT.
   CLEAR W_FCAT.

   LOOP AT T_VBAK INTO W_VBAK.

     W_FCAT-FIELDNAME = W_VBAK-VBELN.
     W_FCAT-COLTEXT = W_VBAK-VBELN.
     APPEND W_FCAT TO T_FCAT.
     CLEAR W_FCAT.

   ENDLOOP.

   CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
     EXPORTING
       IT_FIELDCATALOG = T_FCAT
     IMPORTING
       EP_TABLE        = DY_TABLE.
**************BREAK-POINT.
   ASSIGN DY_TABLE->* TO <DY_TABLE>.
   DATA WA TYPE REF TO DATA.
   CREATE DATA WA LIKE LINE OF <DY_TABLE>.
   ASSIGN WA->* TO <DY_WA>.
ENDFORM.                    " GET_FCAT
*&---------------------------------------------------------------------*
*&      Form  POPULATE_DYN
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM POPULATE_DYN .
   ASSIGN COMPONENT  'KUNNR'  OF STRUCTURE <DY_WA> TO <FIELD>.
   IF SY-SUBRC = 0.
     <FIELD> = W_VBAK-KUNNR.
   ENDIF.

   LOOP AT T_VBAK INTO W_VBAK.
     ASSIGN COMPONENT  W_VBAK-VBELN OF STRUCTURE <DY_WA> TO <FIELD>.
     IF SY-SUBRC = 0.
       <FIELD> = W_VBAK-ERNAM.
     ENDIF.
     AT END OF KUNNR.
       APPEND <DY_WA> TO <DY_TABLE>.
     ENDAT.
   ENDLOOP.

   CALL FUNCTION 'LVC_TRANSFER_TO_SLIS'
     EXPORTING
       IT_FIELDCAT_LVC               = T_FCAT
*   IT_SORT_LVC                   =
*   IT_FILTER_LVC                 =
*   IS_LAYOUT_LVC                 =
    IMPORTING
      ET_FIELDCAT_ALV               = IT_FCAT
*   ET_SORT_ALV                   =
*   ET_FILTER_ALV                 =
*   ES_LAYOUT_ALV                 =
* TABLES
*   IT_DATA                       =
* EXCEPTIONS
*   IT_DATA_MISSING               = 1
*   IT_FIELDCAT_LVC_MISSING       = 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.

   CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
*   I_INTERFACE_CHECK                 = ' '
*   I_BYPASSING_BUFFER                = ' '
*   I_BUFFER_ACTIVE                   = ' '
      I_CALLBACK_PROGRAM                = SY-CPROG
*   I_CALLBACK_PF_STATUS_SET          = ' '
*   I_CALLBACK_USER_COMMAND           = ' '
*   I_CALLBACK_TOP_OF_PAGE            = ' '
*   I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
*   I_CALLBACK_HTML_END_OF_LIST       = ' '
*   I_STRUCTURE_NAME                  =
*   I_BACKGROUND_ID                   = ' '
*   I_GRID_TITLE                      =
*   I_GRID_SETTINGS                   =
*   IS_LAYOUT                         =
       IT_FIELDCAT                       = IT_FCAT
*   IT_EXCLUDING                      =
*   IT_SPECIAL_GROUPS                 =
*   IT_SORT                           =
*   IT_FILTER                         =
*   IS_SEL_HIDE                       =
*   I_DEFAULT                         = 'X'
*   I_SAVE                            = ' '
*   IS_VARIANT                        =
*   IT_EVENTS                         =
*   IT_EVENT_EXIT                     =
*   IS_PRINT                          =
*   IS_REPREP_ID                      =
*   I_SCREEN_START_COLUMN             = 0
*   I_SCREEN_START_LINE               = 0
*   I_SCREEN_END_COLUMN               = 0
*   I_SCREEN_END_LINE                 = 0
*   I_HTML_HEIGHT_TOP                 = 0
*   I_HTML_HEIGHT_END                 = 0
*   IT_ALV_GRAPHICS                   =
*   IT_HYPERLINK                      =
*   IT_ADD_FIELDCAT                   =
*   IT_EXCEPT_QINFO                   =
*   IR_SALV_FULLSCREEN_ADAPTER        =
* IMPORTING
*   E_EXIT_CAUSED_BY_CALLER           =
*   ES_EXIT_CAUSED_BY_USER            =
     TABLES
       T_OUTTAB                          <DY_TABLE> "T_VBAK
* EXCEPTIONS
*   PROGRAM_ERROR                     = 1
*   OTHERS                            = 2
             .
   IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
   ENDIF.



ENDFORM.                    " POPULATE_DYN