Skip to Content
avatar image
Former Member

Adding Specific columns of dynamic internal table row into another column

Hi Gurus,

I need to add few columns of a dynamic internal table row into another column:

Article description hy01 hy02 total
101      panza         10     12      22
102      masht         12     12     24

dynamic internal table is created and columns hy01 hy02.... can increase

How to add the the values in hy01 hy 02... into total.

Regards,

Dep

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Best Answer
    May 26, 2011 at 01:19 PM

    Hi.,

    You declare your Filedcatalog like f1 f2 f3 ... dynamically by adding 1 and concatinating f.

    get the count of fields. let say 3.

    n = cont . " number of fields or columns

    loop at itab into wa.
    do n times.                              " here n = 3.
    
    do the same here.. concatenate f and 1 in to *field*.
    
    assign component  *field*  <field symbol WA> to <fs>
    lv_total = lv_total + <fs>. 
    
    enddo.
    
    assign component Total of <field symbol WA> to <fs_total>
    
    <fs_total> = lv_total.
    
    endloop.

    hope this helps u.,

    Thanks & Regards,

    Kiran

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    May 26, 2011 at 02:32 PM

    Hi,

    If you really want to have a dynamic table, then you will have to find a way to generate a whole new table, and then copy the data from the old table to the new one. There is no way to modify a type during runtime in ABAP.

    Here an example how to generate a dynamic table based on another internal table, hope this will help you.

    TYPE-POOLS: slis.
    
    PARAMETERS: p_nb_hy TYPE i DEFAULT 2. "Number of new HY columns to be added
    
    * Type ZST_T:
    *   matnr  TYPE matnr
    *   maktx  TYPE maktx
    *   hy01   TYPE i
    *   total  TYPE i
    
    TYPES: ty_t TYPE STANDARD TABLE OF zst_s.
    
    PERFORM main.
    
    *&---------------------------------------------------------------------*
    *&      Form  main
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    FORM main.
      DATA: lt_fieldcat     TYPE slis_t_fieldcat_alv,
            lt_t            TYPE ty_t,
            lr_new_t        TYPE REF TO data.
    
      FIELD-SYMBOLS: <lt_new_t> TYPE STANDARD TABLE.
    
      "Add some lines to LT_T just to have something to display on screen
      DO 10 TIMES.
        APPEND INITIAL LINE TO lt_t.
      ENDDO.
    
      CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
        EXPORTING
          i_structure_name = 'ZST_S'
        CHANGING
          ct_fieldcat      = lt_fieldcat.
    
      "Copy LT_T to LR_NEW_T
      PERFORM extend_and_copy_table USING lt_t p_nb_hy CHANGING lr_new_t lt_fieldcat.
      CLEAR lt_t. "Not needed anymore...
    
      ASSIGN lr_new_t->* TO <lt_new_t>.
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
        EXPORTING
          it_fieldcat = lt_fieldcat
        TABLES
          t_outtab    = <lt_new_t>.
    ENDFORM.                    "main
    
    *&---------------------------------------------------------------------*
    *&      Form  extend_and_copy_table
    *&---------------------------------------------------------------------*
    FORM extend_and_copy_table USING ut_t           TYPE STANDARD TABLE
                                     uv_nb_hy       TYPE i
                               CHANGING cr_t        TYPE REF TO data
                                        ct_fieldcat TYPE slis_t_fieldcat_alv
                               RAISING cx_sy_struct_creation cx_sy_table_creation.
    
      DATA: lo_tabledescr      TYPE REF TO cl_abap_tabledescr,
            lo_structdescr     TYPE REF TO cl_abap_structdescr,
            lo_new_structdescr TYPE REF TO cl_abap_structdescr,
            lo_new_tabledescr  TYPE REF TO cl_abap_tabledescr,
            lt_components      TYPE cl_abap_structdescr=>component_table,
            ls_component       TYPE cl_abap_structdescr=>component,
            lv_field_cnt       TYPE numc2,
            ls_fieldcat        TYPE slis_fieldcat_alv,
            lr_fieldcat        TYPE REF TO slis_fieldcat_alv.
    
      FIELD-SYMBOLS: <ls_old_s> TYPE ANY,
                     <lt_new_t> TYPE STANDARD TABLE,
                     <ls_new_s> TYPE ANY.
    
      "Get the list of all components from UT_T line structure
      lo_tabledescr  ?= cl_abap_tabledescr=>describe_by_data( ut_t ).
      lo_structdescr ?= lo_tabledescr->get_table_line_type( ).
      lt_components  = lo_structdescr->get_components( ).
    
      "The new columns will be from type of column HY01
      ls_component-type = lo_structdescr->get_component_type( 'HY01' ).
      "The new columns will have the same fieldcat info as column HY01
      READ TABLE ct_fieldcat INTO ls_fieldcat WITH KEY fieldname = 'HY01'.
    
      "HY<lv_field_cnt> = new field name
      lv_field_cnt = uv_nb_hy + 1.
    
      "For each new column...
      DO uv_nb_hy TIMES.
        "Generate the new column field name
        CONCATENATE  'HY' lv_field_cnt INTO ls_component-name.
        ls_fieldcat-fieldname = ls_component-name.
    
        "Add the new field to the components of the new structure
        INSERT ls_component INTO lt_components INDEX 4.
        "Add the new field's fieldcat info to the fieldcat
        INSERT ls_fieldcat  INTO ct_fieldcat   INDEX 4.
    
        lv_field_cnt = lv_field_cnt - 1.
      ENDDO.
    
      "Adjust the COL_POS from fieldcat
      LOOP AT ct_fieldcat REFERENCE INTO lr_fieldcat.
        lr_fieldcat->col_pos = sy-tabix.
      ENDLOOP.
    
      "Create the new table
      lo_new_structdescr = cl_abap_structdescr=>create( p_components = lt_components ).
      lo_new_tabledescr  = cl_abap_tabledescr=>create( p_line_type = lo_new_structdescr ).
      CREATE DATA cr_t TYPE HANDLE lo_new_tabledescr.
      ASSIGN cr_t->* TO <lt_new_t>.
    
      "Copy all data from old to new table
      LOOP AT ut_t ASSIGNING <ls_old_s>.
        APPEND INITIAL LINE TO <lt_new_t> ASSIGNING <ls_new_s>.
        MOVE-CORRESPONDING <ls_old_s> TO <ls_new_s>.
      ENDLOOP.
    ENDFORM.                    "main
    

    Add comment
    10|10000 characters needed characters exceeded

  • May 26, 2011 at 02:34 PM

    HI,

    Try with this below code. I considered here <FS_TABLE> as dynamic internal table .

    Field:symbols: <FS_DYWA> type any,

    <TEMP> type any.

    data : lv_total(45),

    lv_index type sy-tabix.

    LOOP AT <FS_TABLE> ASSIGNING <FS_DYWA>.

    DO.

    ASSIGN COMPONENT SY-INDEX OF STRUCTURE <FS_DYWA> TO <TEMP>.

    IF SY-SUBRC = 0 and SY-INDEX > 2.

    lv_total = total.

    total = total + <TEMP>.

    ELSEIF SY-INDEX LE 2. "Do nothing as two fields article & descript are not to be added

    ELSE.

    lv_index = sy-index - 2.

    ASSIGN COMPONENT lv_INDEX OF STRUCTURE <FS_DYWA> TO <TEMP>.

    if <TEMP> is assigned.

    total = total - lv_total.

    endif.

    EXIT.

    ENDIF.

    ENDDO.

    Srikanth.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    May 27, 2011 at 05:16 AM

    Hi Kiran & Srikanth,

    Thanks for your reply , I will work on your suggestions and get back.

    Hi Dominique,

    Thanks for your reply, I have already created a dynamic internal table( based on no. of sites hy01 hy02...) I need to add few columns.

    Regards,

    Dep

    Add comment
    10|10000 characters needed characters exceeded