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

Dynamic Itab

How to define Dynamic Itabs?

Can we change the fields names, or define field names at runtime?

Is it possible to add or remove fields from itab at runtime?

Please provide some code for same.

Regards,

Sharayu

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

5 Answers

  • Best Answer
    Posted on Dec 03, 2007 at 06:07 AM

    Hello,

    Try this code, it generates field names dynamically

    * 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-field2 = ''19970814''.'. APPEND inctabl.
    inctabl-line = 'dyntab-field3 = 1.'. APPEND inctabl.
    inctabl-line = 'append dyntab.'. APPEND inctabl.
    inctabl-line = ' '. APPEND inctabl.
    inctabl-line = 'loop at dyntab.'. APPEND inctabl.
    inctabl-line = 'write: / dyntab-field1, dyntab-field2, dyntab-field3.'.
     APPEND inctabl.
    inctabl-line = 'endloop.'. APPEND inctabl.
    
    * Create and run the dynamic program
    INSERT REPORT 'ZDYNPRO' FROM inctabl.
    IF sy-subrc = 0.
      SUBMIT zdynpro.
    
    ELSE.
      WRITE : / 'error'.
    ENDIF.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Dec 03, 2007 at 05:35 AM

    Hi,

    Check the following link:

    http://sap-img.com/ab030.htm

    Regards,

    Bhaskar

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Dec 03, 2007 at 05:50 AM

    Hello Sharayu

    You may have a look at my sample report <b>ZUS_SDN_RTTI_CREATE_STRUCTUR_2</b> in thread:

    Creation of table of table dynamically

    Regards

    Uwe

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Dec 03, 2007 at 05:35 AM

    Hi,

    DATA: lw_fieldvalue(40) TYPE c,

    lw_fieldname(30) TYPE c.

    gwa_output TYPE REF TO data.

    FIELD-SYMBOLS: <gwa_output> TYPE ANY.

    FIELD-SYMBOLS: <l_fvalue> TYPE ANY.

    *---Create a pointer to store the workarea of the final output table.

    CREATE DATA gwa_output LIKE LINE OF <git_output>.(this is the output table)

    *---Assign it to a fieldsymbol which will be used in populating the

    *---final output table.

    ASSIGN gwa_output->* TO <gwa_output>.

    Loop at i_tab into wa_tab.

    move wa_tab-field1 to v_fieldvalue. (moving the first field value into field value variable).

    CONDENSE v_fieldvalue NO-GAPS. (Condense the output).

    CLEAR: <gwa_output>.

    ASSIGN COMPONENT 'FIELD1' OF STRUCTURE <gwa_output> TO <l_fvalue>.

    <l_fvalue> = V_fieldvalue.

    Repeat the same with other fields

    *---Append the current record to final output table.

    APPEND <gwa_output> TO <git_output>.

    endloop.

    You need to assign the component of table <dyn_Tab> to field symbol and than give value to that field symbol.

    Like:

    ASSIGN COMPONENT FLD1 OF STRUCTURE <DYN_TAB> TO <FS>.

    <FS> = I_TAB-FLD1.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Dec 03, 2007 at 05:42 AM

    For Dynamic ITABs

    see

    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.

    for CONTROL BREAK STATEMENTS

    All this AT NEW, AT FIRST, AT END OF and AT LAST are called control break statements of Internal tables and are used to calculate the TOTALS based on sertain key fields in that internal table

    FIrst to use these statements the ITAB has to be sorted by the key fields on whcih you need the SUM of the fields.

    Some time you will get * when moving data from this int table to other table using these commands

    so you have to use

    READ TABLE ITAB INDEX SY-TABIX in AT..ENDAT..if you are using other fields between them

    DATA: sflight_tab TYPE SORTED TABLE OF sflight

    WITH UNIQUE KEY carrid connid fldate,

    sflight_wa LIKE LINE OF sflight_tab.

    SELECT *

    FROM sflight

    INTO TABLE sflight_tab.

    SORT sfllight_tab BY carrid connid .

    LOOP AT sflight_tab INTO sflight_wa.

    AT NEW connid.

    WRITE: / sflight_wa-carrid,

    sflight_wa-connid.

    ULINE.

    ENDAT.

    WRITE: / sflight_wa-fldate,

    sflight_wa-seatsocc.

    AT END OF connid.

    SUM.

    ULINE.

    WRITE: / 'Sum',

    sflight_wa-seatsocc UNDER sflight_wa-seatsocc.

    SKIP.

    ENDAT.

    AT END OF carrid.

    SUM.

    ULINE.

    WRITE: / 'Carrier Sum',

    sflight_wa-seatsocc UNDER sflight_wa-seatsocc.

    NEW-PAGE.

    ENDAT.

    AT LAST.

    SUM.

    WRITE: / 'Overall Sum',

    sflight_wa-seatsocc UNDER sflight_wa-seatsocc.

    ENDAT.

    ENDLOOP.

    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.