Skip to Content
author's profile photo Former Member
Former Member

problem in creating internal table dynamically

Hi All,

I am trying to retireve the operations based on the project number for a material. Based on that i want to build fieldcatalog. So the problem is we dont know how many operations we will have for a project untill runtime. I mean we dont the structure of the internal table. I want to go with dynamic internal tables. But even then also we have to create a table with some known structure to assign it to field symbol. But i dont hw my structure will be as it will be determined at the run time.

Any suggestions on this problem.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Posted on Dec 03, 2007 at 04:16 PM

    hi Ben,

    the way to do it:

    DATA : gw_dynwa TYPE REF TO data.

    DATA : gt_dyntab TYPE REF TO data.

    FIELD-SYMBOLS : <gf_table> TYPE table,

    <gf_line> TYPE ANY.

    1. You have to build a fieldcatalog like it_fieldcatalog of importing parameter of the method in next step (similar to ALV, but not exactly the same)

    2. You have to call method create_dynamic_table of class cl_alv_table_create (this will create the dynamic internal table acc. to the fieldcatalog)

    3.

    • Internal table assignment

    ASSIGN gt_dyntab->* TO <gf_table>.

    • Work area assignment

    CREATE DATA gw_dynwa LIKE LINE OF <gf_table>.

    ASSIGN gw_dynwa->* TO <gf_line>.

    Now you can LOOP through the internal table into the work area. The single fields you can reach through

    ASSIGN COMPONENT fieldname OF STRUCTURE ... statetment.

    hope this helps

    ec

    Add a comment
    10|10000 characters needed characters exceeded

    • this part creates the dynamic internal table:

      pls. check CASE p_zaufr (in FORM create_dynamic_itab). p_zaufr is a parameter from the selection screen which can have value from 1 to 6. The table is built up acc. to this value. For example: if it is 2, than each company code which is selected (also on selection screen) will be one coloum for the internal table

      create a Z program and copy this code (plus the codes I pasted in my previous post), with some modifcation it has to work, do you see in real how does it work

      (you can also check the following program: BCALV_TEST_FB_CREATE, it also uses this method)

      *&---------------------------------------------------------------------*
      *&      Form  create_dynamic_itab
      *&---------------------------------------------------------------------*
      *       text
      *----------------------------------------------------------------------*
      FORM create_dynamic_itab CHANGING lt_data TYPE any.
      
      * Number of dynamic fields
        DATA : lv_counter(2) TYPE n.
      
      * Fieldcat
        DATA : lt_fcat TYPE TABLE OF lvc_s_fcat.
        DATA : lw_fcat TYPE lvc_s_fcat.
      
      * Rules for adding new fields:
      * All static fields : exactly 5 letters long, except:
      * Total fields : 5 letters + 1 or 2
      * Dynamic fields : 5 letters (domain name)
      *                  Underscore
      *                  Domain values (months, company codes, etc.)
      
      * Company code
        IF p_zaufr NE '02'.
          lw_fcat-fieldname = 'BUKRS'.
          lw_fcat-ref_field = 'RBUKRS'.
          lw_fcat-ref_table = 'FAGLFLEXT'.
          APPEND lw_fcat TO lt_fcat.
          CLEAR : lw_fcat.
        ENDIF.
      
      * Currency
        lw_fcat-fieldname = 'RTCUR'.
        lw_fcat-ref_field = 'RTCUR'.
        lw_fcat-ref_table = 'FAGLFLEXT'.
        APPEND lw_fcat TO lt_fcat.
        CLEAR : lw_fcat.
      
      * Text 2
        lw_fcat-fieldname = 'BEZE2'.
        lw_fcat-ref_field = 'BEZEI2'.
        lw_fcat-ref_table = 'ZLIDL_IDLSAP'.
        APPEND lw_fcat TO lt_fcat.
        CLEAR : lw_fcat.
      
      * Line number
        lw_fcat-fieldname = 'ZEILE'.
        lw_fcat-ref_field = 'ZEILE'.
        lw_fcat-ref_table = 'ZLIDL_IDLSAP'.
        APPEND lw_fcat TO lt_fcat.
        CLEAR : lw_fcat.
      
      * Sum line?
        lw_fcat-fieldname = 'SUMZE'.
        lw_fcat-ref_field = 'SUMZEI'.
        lw_fcat-ref_table = 'ZLIDL_KERZEI'.
        APPEND lw_fcat TO lt_fcat.
        CLEAR : lw_fcat.
      
      * Text
        lw_fcat-fieldname = 'BEZE1'.
        lw_fcat-ref_field = 'BEZEI'.
        lw_fcat-ref_table = 'ZLIDL_IDLSAP'.
        APPEND lw_fcat TO lt_fcat.
        CLEAR : lw_fcat.
      
      * Account number
        lw_fcat-fieldname = 'RACCT'.
        lw_fcat-ref_field = 'RACCT'.
        lw_fcat-ref_table = 'FAGLFLEXT'.
        APPEND lw_fcat TO lt_fcat.
        CLEAR : lw_fcat.
      
      * Account text
        lw_fcat-fieldname = 'TXT50'.
        lw_fcat-ref_field = 'TXT50'.
        lw_fcat-ref_table = 'SKAT'.
        APPEND lw_fcat TO lt_fcat.
        CLEAR : lw_fcat.
      
      * Technical field (because of proper underlying)
        lw_fcat-fieldname = 'ZEIL2'.
        lw_fcat-ref_field = 'ZEILE'.
        lw_fcat-ref_table = 'ZLIDL_IDLSAP'.
        APPEND lw_fcat TO lt_fcat.
        CLEAR : lw_fcat.
      
      * Dynamic fields
        CASE p_zaufr.
          WHEN '01'.
            DESCRIBE TABLE gt_t009c LINES gv_nodf.
      * Fiscal periods (12 normal and 1 special)
            LOOP AT gt_t009c INTO gw_t009c WHERE poper LE '013'.
              CONCATENATE 'POPER_' gw_t009c-poper INTO lw_fcat-fieldname.
              lw_fcat-ref_field = 'HSL01'.
              lw_fcat-ref_table = 'FAGLFLEXT'.
              APPEND lw_fcat TO lt_fcat.
              CLEAR : lw_fcat.
            ENDLOOP.
          WHEN '02'.
            DESCRIBE TABLE gt_t001 LINES gv_nodf.
      * Company codes
            LOOP AT gt_t001 INTO gw_t001.
              CONCATENATE 'BUKRS_' gw_t001-bukrs INTO lw_fcat-fieldname.
              lw_fcat-ref_field = 'HSL01'.
              lw_fcat-ref_table = 'FAGLFLEXT'.
              APPEND lw_fcat TO lt_fcat.
              CLEAR : lw_fcat.
            ENDLOOP.
          WHEN '03'.
            gv_nodf = 1.
      * Company code groups
            CONCATENATE 'BUGRP_' p_bugr INTO lw_fcat-fieldname.
            lw_fcat-ref_field = 'HSL01'.
            lw_fcat-ref_table = 'FAGLFLEXT'.
            APPEND lw_fcat TO lt_fcat.
            CLEAR : lw_fcat.
          WHEN '04'.
      * Lagern
            DESCRIBE TABLE gt_lager LINES gv_nodf.
            LOOP AT gt_lager INTO gw_lager.
              CONCATENATE 'VERDI_' gw_lager-verdi INTO lw_fcat-fieldname.
              lw_fcat-ref_field = 'HSL01'.
              lw_fcat-ref_table = 'FAGLFLEXT'.
              APPEND lw_fcat TO lt_fcat.
              CLEAR : lw_fcat.
            ENDLOOP.
          WHEN '05'.
            DESCRIBE TABLE gt_cchir LINES gv_nodf.
      * Cost center groups
            LOOP AT gt_cchir INTO gw_cchir.
              CONCATENATE 'KHINR_' gw_cchir-khinr INTO lw_fcat-fieldname.
              lw_fcat-ref_field = 'HSL01'.
              lw_fcat-ref_table = 'FAGLFLEXT'.
              APPEND lw_fcat TO lt_fcat.
              CLEAR : lw_fcat.
            ENDLOOP.
          WHEN '06'.
            DESCRIBE TABLE gt_tka05 LINES gv_nodf.
      * Cost center types
      * Value fields for cost center types
            LOOP AT gt_tka05 INTO gw_tka05.
              CONCATENATE 'KOSAR_' gw_tka05-kosar INTO lw_fcat-fieldname.
              lw_fcat-ref_field = 'HSL01'.
              lw_fcat-ref_table = 'FAGLFLEXT'.
              APPEND lw_fcat TO lt_fcat.
              CLEAR : lw_fcat.
            ENDLOOP.
        ENDCASE.
      
      * Total amount - Current year
        lw_fcat-fieldname = 'HSLVT1'.
        lw_fcat-ref_field = 'HSLVT'.
        lw_fcat-ref_table = 'FAGLFLEXT'.
        APPEND lw_fcat TO lt_fcat.
        CLEAR : lw_fcat.
      
      * Total amount - Previous year
        lw_fcat-fieldname = 'HSLVT2'.
        lw_fcat-ref_field = 'HSLVT'.
        lw_fcat-ref_table = 'FAGLFLEXT'.
        APPEND lw_fcat TO lt_fcat.
        CLEAR : lw_fcat.
      
      * Colour
        lw_fcat-fieldname = 'COLOR'.
        lw_fcat-datatype  = 'C'.
        lw_fcat-inttype   = 'C'.
        lw_fcat-intlen    = '3'.
        APPEND lw_fcat TO lt_fcat.
        CLEAR : lw_fcat.
      
        CALL METHOD cl_alv_table_create=>create_dynamic_table
          EXPORTING
      *    I_STYLE_TABLE             =
            it_fieldcatalog           = lt_fcat
      *    I_LENGTH_IN_BYTE          =
          IMPORTING
            ep_table                  = lt_data
      *    E_STYLE_FNAME             =
          EXCEPTIONS
            generate_subpool_dir_full = 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.                    "create_dynamic_itab

  • Posted on Dec 03, 2007 at 04:48 PM

    Hi Ben,

    I've created one for my requirement, just copy and paste for better understanding how dynamic table concept will work..

    *&---------------------------------------------------------------------*
    *& Report  YTST5
    *&
    *&---------------------------------------------------------------------*
    *&
    *&
    *&---------------------------------------------------------------------*
    
    REPORT  YTST5.
    
    
    *{PAVAN
    *ALV data declarations
    TYPE-POOLS: slis ,tstr.
    DATA: fieldcatalog TYPE slis_t_fieldcat_alv WITH HEADER LINE,
          gd_tab_group TYPE slis_t_sp_group_alv,
          gd_layout    TYPE slis_layout_alv,
          gd_repid     LIKE sy-repid.
    *}PAVAN
    
    *** Tables
    DATA: LT_DATA type ref to DATA.
    DATA: LT_FIELDCATALOG type LVC_T_FCAT.
    
    *** Structure
    DATA: LS_FIELDCATALOG type LVC_S_FCAT.
    
    *** Data References
    DATA: NEW_LINE type ref to data.
    
    *** Field Symbols
    FIELD-SYMBOLS: <FS_DATA> type ref to DATA,
                   <FS_1> type any table,
                   <FS_2>,
                   <FS_3>.
    
    selection-screen begin of block b1.
    parameters: p_weeks type i.
    selection-screen end of block b1.
    
    *Populating the internal table with fieldnames required for our dynamic
    *internal table
    data: l_Str(10).
    data: l_nos type i.
    l_nos = 1.
    
    while l_nos le p_weeks.
    l_str = 'Week' .
    l_str+4(2) = l_nos.
    ls_fieldcatalog-fieldname = l_Str.
    ls_fieldcatalog-inttype = 'QUAN'.
    append LS_FIELDCATALOG to LT_FIELDCATALOG.
    
    l_nos = l_nos + 1.
    endwhile.
    
    *LS_FIELDCATALOG-FIELDNAME = 'MANDT'.
    *append LS_FIELDCATALOG to LT_FIELDCATALOG.
    *
    *LS_FIELDCATALOG-FIELDNAME = 'CARRID'. "Fieldname
    *LS_FIELDCATALOG-INTTYPE = 'C'. "Internal Type C-> Character
    *append LS_FIELDCATALOG to LT_FIELDCATALOG.
    *
    *LS_FIELDCATALOG-FIELDNAME = 'CONNID'.
    *LS_FIELDCATALOG-INTTYPE = 'N'.
    *append LS_FIELDCATALOG to LT_FIELDCATALOG.
    *
    *LS_FIELDCATALOG-FIELDNAME = 'FLDATE'.
    *LS_FIELDCATALOG-INTTYPE = 'D'.
    *append LS_FIELDCATALOG to LT_FIELDCATALOG.
    *
    *LS_FIELDCATALOG-FIELDNAME = 'PRICE'.
    *LS_FIELDCATALOG-INTTYPE = 'P'.
    *append LS_FIELDCATALOG to LT_FIELDCATALOG.
    *
    *LS_FIELDCATALOG-FIELDNAME = 'CURRENCY'.
    *LS_FIELDCATALOG-INTTYPE = 'C'.
    *append LS_FIELDCATALOG to LT_FIELDCATALOG.
    *
    
    *Assigning Field-Symbol to our dynamic internal table
    assign LT_DATA to <FS_DATA>.
    
    *Calling the method CREATE_DYNAMIC_TABLE
    call method cl_alv_table_create=>create_dynamic_table
         exporting
           it_fieldcatalog = LT_FIELDCATALOG
         importing
           ep_table = <FS_DATA>
         exceptions
           generate_subpool_dir_full = 1
           others = 2
    		.
    if sy-subrc <> 0.
    endif.
    
    
    *** So <FS_1> now points to our dynamic internal table.
    
    assign <FS_DATA>->* to <FS_1>.
    
    *** Next step is to create a work area for our dynamic internal table.
    
    create data NEW_LINE like line of <FS_1>.
    
    *** A field-symbol to access that work area
    assign NEW_LINE->*  to <FS_2>.
    
    assign component 1 of structure <FS_2> to <FS_3>.
    <FS_3> = 10.
    assign component 2 of structure <FS_2> to <FS_3>.
    <FS_3> = 20.
    insert <FS_2> into table <FS_1>.
    
    clear <FS_3>.
    assign component 1 of structure <FS_2> to <FS_3>.
    <FS_3> = 100.
    
    assign component 2 of structure <FS_2> to <FS_3>.
    <FS_3> = 200.
    insert <FS_2> into table <FS_1>.
    
    *** And to put the data in the internal table
    *select MANDT CARRID CONNID FLDATE PRICE CURRENCY
    *  from SFLIGHT
    *  into corresponding fields of table <FS_1>.
    
    
    
    perform build_field_catalog.
    
    field-symbols: <itab> type table.
    
    assign <FS_1> to <itab>.
    
    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
        EXPORTING
          i_callback_program     = gd_repid
          it_fieldcat            = fieldcatalog[]
          i_save                 = 'X'
        TABLES
          t_outtab                          = <itab>
    * 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.
    
    
    
    form build_field_catalog.
    
    l_nos = 1.
    
    while l_nos le p_weeks.
    l_str = 'WEEK' .
    l_str+4(2) = l_nos.
    *{FIELD CATALOG
      fieldcatalog-fieldname   = l_str.
      fieldcatalog-seltext_m   = l_str.
      fieldcatalog-col_pos     = l_nos.
      fieldcatalog-outputlen   = 10.
      fieldcatalog-emphasize   = 'X'.
      fieldcatalog-key         = 'X'.
      APPEND fieldcatalog TO fieldcatalog.
      CLEAR  fieldcatalog.
    *}FIELD CATALOG
    
    l_nos = l_nos + 1.
    endwhile.
    
    
    *  fieldcatalog-fieldname   = 'WEEK1'.
    *  fieldcatalog-seltext_m   = 'Week1'.
    *  fieldcatalog-col_pos     = 0.
    *  fieldcatalog-outputlen   = 10.
    *  fieldcatalog-emphasize   = 'X'.
    *  fieldcatalog-key         = 'X'.
    *  APPEND fieldcatalog TO fieldcatalog.
    *  CLEAR  fieldcatalog.
    *
    *  fieldcatalog-fieldname   = 'WEEK2'.
    *  fieldcatalog-seltext_m   = 'Week 2'.
    *  fieldcatalog-col_pos     = 1.
    *  APPEND fieldcatalog TO fieldcatalog.
    *  CLEAR  fieldcatalog.
    
    endform.
    
    Thanks
    Pavan
    
    

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Dec 03, 2007 at 06:02 PM

    Hi ben,

    did you do any search on your question?

    This topic is one of the most-discussed ones so that you find more information even here:

    http://www.google.com/search?hl=de&q=problemincreatinginternaltabledynamically&btnG=Google-Suche&meta=lr%3Dlang_de%7Clang_en">problem in creating internal table dynamically</a>

    Regards,

    Clemens

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.