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

Dynamic addition of fields to internal table

Hi all,

I have an internal table itab_data with some fields in it.

I need to add few more fields to this internal table during run-time based on some conditions. All the fields to be added is of same data type . But the number of fields vary based on input.

Itab_data contains

matnr type mara-matnr

maktx type makt-maktx.

Now, based on input i need to add few more fields of type AUSP-ATWRT.

The field names are like atwrt1 , atwrt2...atwrt n ..

Hope my question is clear..

Thanking you in advance ..

Shankara Narayanan T.V

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

6 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Oct 20, 2006 at 12:30 PM

    Hi

    U can't change the structure of an internal table dynamically, u should create a new table with the fields of the old table and the new fields.

    U can use the method CREATE_DYNAMIC_TABLE of class CL_ALV_TABLE_CREATE to create a table dynamically.

    After creating it you have to transfer all data from old to new table.

    Max

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Hi

      Try this example, I hope It can be helpful for you:

      PARAMETERS: P1 RADIOBUTTON GROUP R1 DEFAULT 'X',

      P2 RADIOBUTTON GROUP R1,

      P3 RADIOBUTTON GROUP R1,

      P4 RADIOBUTTON GROUP R1.

      DATA: BEGIN OF ITAB OCCURS 0,

      MATNR LIKE MARA-MATNR,

      MAKTX LIKE MAKT-MAKTX,

      END OF ITAB.

      • ITAB IS

      DATA: MY_TABLE TYPE REF TO DATA.

      FIELD-SYMBOLS: <FS_TABLE> TYPE TABLE.

      DATA: MY_WA TYPE REF TO DATA.

      FIELD-SYMBOLS: <WA> TYPE ANY.

      DATA: LS_ALV_CAT TYPE LVC_S_FCAT.

      DATA: LT_ALV_CAT TYPE LVC_T_FCAT.

      FIELD-SYMBOLS: <VALUE> TYPE ANY.

      DO 10 TIMES.

      MOVE SY-INDEX TO ITAB-MATNR.

      ITAB-MAKTX = 'Test'.

      APPEND ITAB.

      ENDDO.

      LS_ALV_CAT-FIELDNAME = 'MATNR'.

      LS_ALV_CAT-TABNAME = 'MY_ITAB'.

      LS_ALV_CAT-REF_FIELD = 'MATNR'.

      LS_ALV_CAT-REF_TABLE = 'MARA'.

      APPEND LS_ALV_CAT TO LT_ALV_CAT.

      LS_ALV_CAT-FIELDNAME = 'MAKTX'.

      LS_ALV_CAT-TABNAME = 'MY_ITAB'.

      LS_ALV_CAT-REF_FIELD = 'MAKTX'.

      LS_ALV_CAT-REF_TABLE = 'MAKT'.

      CASE 'X'.

      WHEN P1.

      • Add 1 field

      LS_ALV_CAT-FIELDNAME = 'ATWRT1'.

      LS_ALV_CAT-TABNAME = 'MY_ITAB'.

      LS_ALV_CAT-REF_FIELD = 'ATWRT'.

      LS_ALV_CAT-REF_TABLE = 'AUSP'.

      APPEND LS_ALV_CAT TO LT_ALV_CAT.

      WHEN P2.

      • Add 1 field

      LS_ALV_CAT-FIELDNAME = 'ATWRT1'.

      LS_ALV_CAT-TABNAME = 'MY_ITAB'.

      LS_ALV_CAT-REF_FIELD = 'ATWRT'.

      LS_ALV_CAT-REF_TABLE = 'AUSP'.

      APPEND LS_ALV_CAT TO LT_ALV_CAT.

      LS_ALV_CAT-FIELDNAME = 'ATWRT2'.

      APPEND LS_ALV_CAT TO LT_ALV_CAT.

      WHEN P3.

      • Add 3 field

      LS_ALV_CAT-FIELDNAME = 'ATWRT1'.

      LS_ALV_CAT-TABNAME = 'MY_ITAB'.

      LS_ALV_CAT-REF_FIELD = 'ATWRT'.

      LS_ALV_CAT-REF_TABLE = 'AUSP'.

      APPEND LS_ALV_CAT TO LT_ALV_CAT.

      LS_ALV_CAT-FIELDNAME = 'ATWRT2'.

      APPEND LS_ALV_CAT TO LT_ALV_CAT.

      LS_ALV_CAT-FIELDNAME = 'ATWRT3'.

      APPEND LS_ALV_CAT TO LT_ALV_CAT.

      WHEN P4.

      • Add 4 field

      LS_ALV_CAT-FIELDNAME = 'ATWRT1'.

      LS_ALV_CAT-TABNAME = 'MY_ITAB'.

      LS_ALV_CAT-REF_FIELD = 'ATWRT'.

      LS_ALV_CAT-REF_TABLE = 'AUSP'.

      APPEND LS_ALV_CAT TO LT_ALV_CAT.

      LS_ALV_CAT-FIELDNAME = 'ATWRT2'.

      APPEND LS_ALV_CAT TO LT_ALV_CAT.

      LS_ALV_CAT-FIELDNAME = 'ATWRT3'.

      APPEND LS_ALV_CAT TO LT_ALV_CAT.

      LS_ALV_CAT-FIELDNAME = 'ATWRT4'.

      APPEND LS_ALV_CAT TO LT_ALV_CAT.

      ENDCASE.

      • Create table

      CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE

      EXPORTING

      IT_FIELDCATALOG = LT_ALV_CAT

      IMPORTING

      EP_TABLE = MY_TABLE.

      ASSIGN MY_TABLE->* TO <FS_TABLE>.

      • Create workarea

      CREATE DATA MY_WA LIKE LINE OF <FS_TABLE>.

      ASSIGN MY_WA->* TO <WA>.

      LOOP AT ITAB.

      • Assign old data

      MOVE ITAB TO <WA>.

      • Assign new value

      CASE 'X'.

      WHEN P1.

      ASSIGN COMPONENT 'ATWRT1' OF STRUCTURE <WA> TO <VALUE>.

      MOVE SY-TABIX TO <VALUE>.

      WHEN P2.

      ASSIGN COMPONENT 'ATWRT1' OF STRUCTURE <WA> TO <VALUE>.

      MOVE SY-TABIX TO <VALUE>.

      SY-TABIX = SY-TABIX * 2.

      ASSIGN COMPONENT 'ATWRT2' OF STRUCTURE <WA> TO <VALUE>.

      MOVE SY-TABIX TO <VALUE>.

      WHEN P3.

      ASSIGN COMPONENT 'ATWRT1' OF STRUCTURE <WA> TO <VALUE>.

      MOVE SY-TABIX TO <VALUE>.

      SY-TABIX = SY-TABIX * 2.

      ASSIGN COMPONENT 'ATWRT2' OF STRUCTURE <WA> TO <VALUE>.

      MOVE SY-TABIX TO <VALUE>.

      SY-TABIX = SY-TABIX * 3.

      ASSIGN COMPONENT 'ATWRT3' OF STRUCTURE <WA> TO <VALUE>.

      MOVE SY-TABIX TO <VALUE>.

      WHEN P4.

      ASSIGN COMPONENT 'ATWRT1' OF STRUCTURE <WA> TO <VALUE>.

      MOVE SY-TABIX TO <VALUE>.

      SY-TABIX = SY-TABIX * 2.

      ASSIGN COMPONENT 'ATWRT2' OF STRUCTURE <WA> TO <VALUE>.

      MOVE SY-TABIX TO <VALUE>.

      SY-TABIX = SY-TABIX * 3.

      ASSIGN COMPONENT 'ATWRT3' OF STRUCTURE <WA> TO <VALUE>.

      MOVE SY-TABIX TO <VALUE>.

      SY-TABIX = SY-TABIX * 4.

      ASSIGN COMPONENT 'ATWRT4' OF STRUCTURE <WA> TO <VALUE>.

      MOVE SY-TABIX TO <VALUE>.

      ENDCASE.

      APPEND <WA> TO <FS_TABLE>.

      ENDLOOP.

      LOOP AT <FS_TABLE> ASSIGNING <WA>.

      WRITE: / <WA>.

      ENDLOOP.

      Max

  • author's profile photo Former Member
    Former Member
    Posted on Oct 20, 2006 at 12:18 PM

    Hi ,

    declare the fields in ur itab. like as u r saying ausp-atwrt.

    now in the debugging put a break point near ur itab.

    here u have insert , change , delete options for the itab.

    click on an entry and enter the atwrt in that line and now press change ..

    if u see the line populated with ur new field then i think u r on right track ..

    but the itab structure must contain the field auart in the structure other wise it cant hold ..

    regards,

    VIjay.

    now try to add a field to this

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 20, 2006 at 12:21 PM

    Hi Shankara,

    you can make an internal table in an internal table:

    Data: begin of itab occurs 0,

    matnr like mara-matnr,

    maktx like makt-maktx,

    begin of itab1 occurs 0,

    atwrt like ausp-atwrt,

    end of itab1,

    end of itab.

    ...

    loop at itab.

    write: / itab-matnr, itab-maktx.

    loop at itab-itab1.

    write: itab-itab1-atwrt.

    endloop.

    endloop.

    Regards, dieter

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 20, 2006 at 12:25 PM

    I am not sure of if we can do that dynamically ...but I can put forth another soln as below.

    1. Add two fields to your internal table itab_data.

    ie serial(2) type n and

    data(10) type c.

    2. Populate the data for the material characterstics with incrementing the serial number.

    3. Manipulate the data while writing the output to print/write in as columns.

    Loop at itab_data.

    at new maktx.

    write : / matnr, maktx.

    clear ltext.

    endat.

    lcnt = itab_data-serial - 1.

    lcnt = lcnt * 10. (display length for charc)

    write itab_data-data(10) to ltext+lcnt(10).

    at end of maktx.

    write ltext.

    endat.

    endloop.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 20, 2006 at 12:44 PM

    Hi Shankara,

    i made a little mistake in my example.

    Here a working code:

    REPORT ZGRO_TEST1 MESSAGE-ID ZZ.

    *

    TABLES: MARA, MAKT.

    *

    TYPES: BEGIN OF ITABX,

    ATWRT LIKE AUSP-ATWRT,

    END OF ITABX.

    *

    DATA: WA_ITAB1 TYPE ITABX.

    *

    DATA: BEGIN OF ITAB OCCURS 0,

    MATNR LIKE MARA-MATNR,

    MAKTX LIKE MAKT-MAKTX,

    ITAB1 TYPE TABLE OF ITABX,

    END OF ITAB.

    *

    START-OF-SELECTION.

    *

    SELECT * FROM MARA UP TO 10 ROWS.

    *

    CLEAR: ITAB.

    REFRESH: ITAB-ITAB1.

    *

    SELECT SINGLE * FROM MAKT WHERE MATNR = MARA-MATNR

    AND SPRAS = SY-LANGU.

    *

    ITAB-MATNR = MARA-MATNR.

    ITAB-MAKTX = MAKT-MAKTX.

    <b>* Fill this like you want

    WA_ITAB1-ATWRT = '100'.

    APPEND WA_ITAB1 TO ITAB-ITAB1.

    WA_ITAB1-ATWRT = '200'.

    APPEND WA_ITAB1 TO ITAB-ITAB1.

    WA_ITAB1-ATWRT = '300'.

    APPEND WA_ITAB1 TO ITAB-ITAB1.

    WA_ITAB1-ATWRT = '400'.

    APPEND WA_ITAB1 TO ITAB-ITAB1.</b>*

    APPEND ITAB.

    *

    ENDSELECT.

    *

    LOOP AT ITAB.

    WRITE: / ITAB-MATNR, ITAB-MAKTX.

    LOOP AT ITAB-ITAB1 INTO WA_ITAB1.

    WRITE: WA_ITAB1-ATWRT.

    ENDLOOP.

    ENDLOOP.

    *

    END-OF-SELECTION.

    Regards, Dieter

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 20, 2006 at 01:44 PM

    Hi Shankara,

    do you use

    REUSE_ALV_HIERSEQ_LIST_DISPLAY, or

    REUSE_ALV_LIST_DISPLAY, or

    REUSE_ALV_GRID_DISPLAY or OO.

    With REUSE_ALV_HIERSEQ_LIST_DISPLAY i think there are

    no Problems.

    Regards, Dieter

    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.