Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Problem with Dynamic Table

Hello Experts,

I have a problem with dynamic internal table.

My requirement is to need to display the number of columns based upon the number of cost transfers done for the a WBS element. So I have created a DB table which will update the table during the cost transfer.

The Table will look like

POSIDFROM                POSIDTO                  VERSION KSTAR        WTG001                WTG002

P.1784.010.0.9           P.1784.010.0.1           0001    4001017000                 298,00                 98,00
P.1784.010.0.9           P.1784.010.0.8           0001    4001010000                 300,00                200,00
P.1784.010.0.9           P.1784.010.0.8           0001    4001017000                 298,00                200,00
P.1784.010.0.9           P.1784.010.0.5           0001    4001010000                 300,00                100,00
P.1784.010.0.9           P.1784.010.0.8           0002    4001010000                 300,00                150,00
P.1784.010.0.9           P.1784.010.0.8           0002    4001017000                 298,00                150,00
P.1784.010.0.9           P.1784.010.0.5           0002    4001010000                 300,00                150,00
P.1784.010.0.9           P.1784.010.0.1           0002    4001017000                 298,00                148,00
P.1784.010.0.9           P.1784.010.0.1           0003    4001017000                 298,00                198,00
P.1784.010.0.9           P.1784.010.0.8           0003    4001017000                 298,00                100,00

Here I maintained the column Version. In the above example for the WBS element P.1784.010.0.9 there are three version so I need show three cloumns.

If the other WBS element has 2 version then 2 columns should be shown.

I have created a program like this. But I am not able to move the correct data to the dynamic internal table.

Please give me some idea.

My report is:

REPORT ZV_DYNAMIC_1 .

TYPES: BEGIN OF TTAB,
       REC(1000) TYPE C,
       END OF TTAB.

TYPES: BEGIN OF TDAT,
       FLD1(10) TYPE C,
       FLD2(10) TYPE C,
       FLD3(10) TYPE C,
       END OF TDAT.

DATA: ITAB TYPE TABLE OF TTAB WITH HEADER LINE.
DATA: IDAT TYPE TABLE OF TDAT WITH HEADER LINE.

TYPE-POOLS : ABAP, SLIS.

FIELD-SYMBOLS: <DYN_TABLE> TYPE STANDARD TABLE,
               <DYN_WA>,
               <DYN_FIELD>.

DATA: NEW_TABLE TYPE REF TO DATA,
      NEW_LINE  TYPE REF TO DATA,
      WA_IT_FLDCAT TYPE LVC_S_FCAT,
      IT_FLDCAT TYPE LVC_T_FCAT..




DATA: ITAB1 LIKE Z48R_FEHLER_KOST OCCURS 0 WITH HEADER LINE.
DATA: WA_ITAB1 LIKE ITAB1.
DATA: ISTR_LINES TYPE I,
      BIGGEST_STRING TYPE I,
      INDEX(4) TYPE C.

FIELD-SYMBOLS: <FS_DATA> TYPE REF TO DATA,
               <FS_1> TYPE TABLE,
               <FS_2> TYPE ANY,
               <FS_3> TYPE ANY,
               <FS_5> TYPE ANY.


DATA: LEN TYPE I.

START-OF-SELECTION.
  SELECT * FROM Z48R_FEHLER_KOST INTO TABLE ITAB1.
  LOOP AT ITAB1.
    LEN = STRLEN( ITAB1-POSIDTO ).
    SUBTRACT 1 FROM LEN.
    IF ITAB1-POSIDTO+LEN(1) NE '8'.
      DELETE ITAB1.
    ENDIF.
  ENDLOOP.
  SORT ITAB1 BY VERSION DESCENDING.
  READ TABLE ITAB1 INDEX 1.

* Sales Order Number
  CLEAR WA_IT_FLDCAT.
  WA_IT_FLDCAT-TABNAME   = 'PRPS'.
  WA_IT_FLDCAT-FIELDNAME = 'POSIDFROM'.
  WA_IT_FLDCAT-SELTEXT = 'WBS Element'.
  WA_IT_FLDCAT-COL_POS   = 1.
  WA_IT_FLDCAT-KEY = 'X'.
  APPEND WA_IT_FLDCAT TO IT_FLDCAT .
  CLEAR  WA_IT_FLDCAT.
  CLEAR WA_IT_FLDCAT.
  WA_IT_FLDCAT-TABNAME   = 'PRPS'.
  WA_IT_FLDCAT-FIELDNAME = 'POST1'.
  WA_IT_FLDCAT-SELTEXT = 'Description'.
  WA_IT_FLDCAT-COL_POS   = 2.
  WA_IT_FLDCAT-KEY = 'X'.
  APPEND WA_IT_FLDCAT TO IT_FLDCAT .
  CLEAR  WA_IT_FLDCAT.
  CLEAR WA_IT_FLDCAT.
  WA_IT_FLDCAT-TABNAME   = 'Z48R_FEHLER_KOST'.
  WA_IT_FLDCAT-FIELDNAME = 'VERSION'.
  WA_IT_FLDCAT-SELTEXT = 'Version'.
  WA_IT_FLDCAT-COL_POS   = 3.
  WA_IT_FLDCAT-KEY = SPACE.
  APPEND WA_IT_FLDCAT TO IT_FLDCAT .
  CLEAR  WA_IT_FLDCAT.

  WA_IT_FLDCAT-TABNAME   = 'CSKA'.
  WA_IT_FLDCAT-FIELDNAME = 'KSTAR'.
  WA_IT_FLDCAT-SELTEXT = 'Cost element'.
  WA_IT_FLDCAT-COL_POS   = 4.
  WA_IT_FLDCAT-KEY = SPACE.
  APPEND WA_IT_FLDCAT TO IT_FLDCAT .
  CLEAR  WA_IT_FLDCAT.
  WA_IT_FLDCAT-TABNAME   = 'Z48R_FEHLER_KOST'.
  WA_IT_FLDCAT-FIELDNAME = 'WTG001'.
  WA_IT_FLDCAT-SELTEXT = 'Overall value in transaction currency'.
  WA_IT_FLDCAT-COL_POS   = 5.
  WA_IT_FLDCAT-KEY = SPACE.
  APPEND WA_IT_FLDCAT TO IT_FLDCAT .
  CLEAR  WA_IT_FLDCAT.

  WA_IT_FLDCAT-TABNAME   = 'Z48R_FEHLER_KOST'.
  WA_IT_FLDCAT-FIELDNAME = 'WTG002'.
  WA_IT_FLDCAT-SELTEXT = 'Overall value in transaction currency'.
  WA_IT_FLDCAT-COL_POS   = 6.
  WA_IT_FLDCAT-KEY = SPACE.
  APPEND WA_IT_FLDCAT TO IT_FLDCAT .
  CLEAR  WA_IT_FLDCAT.

* build fieldcat per how many fields are needed from the biggest string.
  BIGGEST_STRING = ITAB1-VERSION.
  DATA: LV_INDEX TYPE I VALUE 6.

  DO BIGGEST_STRING TIMES.
    ADD 1 TO LV_INDEX.
    CLEAR WA_IT_FLDCAT.
    INDEX = SY-INDEX.
    CONCATENATE 'FLD' INDEX INTO WA_IT_FLDCAT-FIELDNAME.
    CONDENSE WA_IT_FLDCAT-FIELDNAME NO-GAPS.
    WA_IT_FLDCAT-DATATYPE = 'C'.
    WA_IT_FLDCAT-INTTYPE =  'C'.
    WA_IT_FLDCAT-INTLEN = '15'.
    WA_IT_FLDCAT-COL_POS   = LV_INDEX.
    APPEND WA_IT_FLDCAT TO IT_FLDCAT .
  ENDDO.
  DATA: LT_DATA TYPE REF TO DATA.
**dynamic table creation for data
  ASSIGN LT_DATA TO <FS_DATA>.
* 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        = <FS_DATA>.

  ASSIGN NEW_TABLE->* TO <DYN_TABLE>.

* Create dynamic work area and assign to FS
  ASSIGN <FS_DATA>->* TO <FS_1>.
  CREATE DATA NEW_LINE LIKE LINE OF <FS_1>.
*** A field-symbol to access that work area
  ASSIGN NEW_LINE->*  TO <FS_2>.

  DATA: FIELDNAME(10).

* Now fill dynamic internal table.
  LOOP AT ITAB1 INTO WA_ITAB1.

    LOOP AT IT_FLDCAT INTO WA_IT_FLDCAT.
      ASSIGN COMPONENT WA_IT_FLDCAT-FIELDNAME OF STRUCTURE
                                              WA_ITAB1 TO <FS_5>.
      IF SY-SUBRC = 0.
        ASSIGN COMPONENT WA_IT_FLDCAT-FIELDNAME OF STRUCTURE
                                                <FS_2> TO <FS_3>.
        <FS_3> = <FS_5>.
      ENDIF.
    ENDLOOP.
    INSERT <FS_2> INTO TABLE <FS_1>.
  ENDLOOP.

  SORT <FS_1>." BY KSTAR.
  LOOP AT <FS_1> ASSIGNING <FS_2>.

    DO.
      INDEX = SY-INDEX.
      CONCATENATE 'FLD' INDEX INTO FIELDNAME.
      CONDENSE FIELDNAME NO-GAPS.
      ASSIGN COMPONENT  FIELDNAME
         OF STRUCTURE  <FS_2>  TO <FS_5>.


      IF SY-SUBRC <> 0.
        EXIT.
      ENDIF.
      IF SY-INDEX = 1.
        WRITE:/ <FS_5>.
      ELSE.
        WRITE:<FS_5>.
      ENDIF.
    ENDDO.
  ENDLOOP.

Thanks in Advance.

Vasanth

Tags:
Former Member

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question