Skip to Content
0
Former Member
Apr 22, 2009 at 10:29 AM

table update from internal table

29 Views

hi,

DATA : BEGIN OF ITAB OCCURS 0.
        INCLUDE STRUCTURE WB2_V_MKPF_MSEG2.
           DATA : END OF ITAB.

         DATA : BEGIN OF IT_ZTPLSTOCK OCCURS 0.
        INCLUDE STRUCTURE ZTPLSTOCK.
       DATA : END OF IT_ZTPLSTOCK.

        DATA : BEGIN OF ITAB_NEW OCCURS 0.
        INCLUDE STRUCTURE WB2_V_MKPF_MSEG2.
        DATA : END OF ITAB_NEW.

DATA : BEGIN OF IT_FINAL OCCURS 0,
           MATNR LIKE ZTPLSTOCK-MATNR,
           WERKS LIKE ZTPLSTOCK-WERKS,
           ZMONTH LIKE ZTPLSTOCK-ZMONTH,
           MJAHR LIKE ZTPLSTOCK-MJAHR,
           MENGE1 LIKE ZTPLSTOCK-MENGE1,
           MEINS1 LIKE ZTPLSTOCK-MEINS1,
           DMBTR1 LIKE ZTPLSTOCK-DMBTR1,
           MENGE2 LIKE ZTPLSTOCK-MENGE2,
           MEINS2 LIKE ZTPLSTOCK-MEINS2,
           DMBTR2 LIKE ZTPLSTOCK-DMBTR2,
         END OF IT_FINAL.

DATA : IT_FINAL1 LIKE IT_FINAL OCCURS 0 WITH HEADER LINE.
DATA DATE LIKE SY-DATUM.
DATA DATE1 LIKE SY-DATUM.
DATA DATE2 LIKE SY-DATUM.
DATA DATE3 LIKE SY-DATUM.
DATA BUDAT LIKE SY-DATUM.
DATA BEDAT LIKE SY-DATUM.

   DATA : D1(4),
       D2(2),
       D3(2).

DATA: BEGIN OF MONTH OCCURS 4,
       SIGN(1),
       OPTION(2),
       LOW(8),
       HIGH(8),
      END OF MONTH   .

SELECT-OPTIONS: S_WERKS FOR WB2_V_MKPF_MSEG2-WERKS_I.
PARAMETERS:     S_MJAHR LIKE WB2_V_MKPF_MSEG2-MJAHR OBLIGATORY.
SELECT-OPTIONS: S_MATNR FOR WB2_V_MKPF_MSEG2-MATNR_I OBLIGATORY.
SELECT-OPTIONS: S_MONTH FOR T549M-PCMTH.


CONCATENATE S_MJAHR S_MONTH-LOW '01' INTO DATE.
CONCATENATE S_MJAHR S_MONTH-HIGH '01' INTO DATE3.

IF S_MONTH-HIGH IS INITIAL.

  CALL FUNCTION 'BKK_GET_MONTH_LASTDAY'
    EXPORTING
      I_DATE = DATE
    IMPORTING
      E_DATE = DATE1.

  MONTH-SIGN = 'I'.
  MONTH-OPTION = 'BT'.
  MONTH-LOW = DATE.
  MONTH-HIGH = DATE1.

  APPEND MONTH.


ELSE.

  CALL FUNCTION 'BKK_GET_MONTH_LASTDAY'
    EXPORTING
      I_DATE = DATE
    IMPORTING
      E_DATE = DATE1.


  CALL FUNCTION 'BKK_GET_MONTH_LASTDAY'
    EXPORTING
      I_DATE = DATE3
    IMPORTING
      E_DATE = DATE2.

  MONTH-SIGN = 'I'.
  MONTH-OPTION = 'BT'.
  MONTH-LOW = DATE.
  MONTH-HIGH = DATE2.

  APPEND MONTH.

ENDIF.

DATA: QUAN1 LIKE MSEG-MENGE.
DATA: QUAN2 LIKE MSEG-MENGE.
DATA: QUAN3 LIKE MSEG-MENGE.
DATA: QUAN4 LIKE MSEG-MENGE.
DATA: QUAN5 LIKE MSEG-MENGE.
DATA: QUAN6 LIKE MSEG-MENGE.
DATA: AMT1 LIKE MSEG-DMBTR.
DATA: AMT2 LIKE MSEG-DMBTR.
DATA: AMT3 LIKE MSEG-DMBTR.
DATA: AMT4 LIKE MSEG-DMBTR.
DATA: AMT5 LIKE MSEG-DMBTR.
DATA: AMT6 LIKE MSEG-DMBTR.

START-OF-SELECTION.

 SELECT * FROM WB2_V_MKPF_MSEG2 INTO CORRESPONDING FIELDS OF TABLE ITAB
                           WHERE WERKS_I IN S_WERKS
                           AND   MJAHR_I EQ S_MJAHR
                           AND   MATNR_I IN S_MATNR
                           AND   BUDAT IN MONTH.

  LOOP AT ITAB.
    MOVE : ITAB-MATNR_I TO IT_FINAL-MATNR,
           ITAB-WERKS_I TO IT_FINAL-WERKS,
           ITAB-MJAHR_I TO IT_FINAL-MJAHR,
           ITAB-MENGE_I TO IT_FINAL-MENGE,
           ITAB-DMBTR_I TO IT_FINAL-DMBTR.
    APPEND IT_FINAL.
    CLEAR IT_FINAL.
  ENDLOOP.

  SELECT * FROM WB2_V_MKPF_MSEG2 INTO CORRESPONDING FIELDS OF TABLE
    ITAB_NEW FOR ALL ENTRIES IN IT_FINAL WHERE WERKS_I = IT_FINAL-WERKS
                             AND   MJAHR_I = IT_FINAL-MJAHR
                             AND   MATNR_I = IT_FINAL-MATNR
                             AND   BUDAT IN MONTH.


  LOOP AT IT_FINAL.
    MOVE-CORRESPONDING IT_FINAL TO IT_FINAL1.
    LOOP AT ITAB_NEW WHERE WERKS_I = IT_FINAL-WERKS
                        AND   MJAHR_I = IT_FINAL-MJAHR
                        AND   MATNR_I = IT_FINAL-MATNR
                        AND   BUDAT IN MONTH.

      IF ITAB_NEW-SHKZG_I = 'H'.
        ITAB_NEW-MENGE_I = ITAB_NEW-MENGE_I * -1.
        ITAB_NEW-DMBTR_I = ITAB_NEW-DMBTR_I * -1.
      ELSE.
        ITAB_NEW-MENGE_I = ITAB_NEW-MENGE_I.
        ITAB_NEW-DMBTR_I = ITAB_NEW-DMBTR_I.
      ENDIF.

      IF ITAB_NEW-BWART_I = '101' OR ITAB_NEW-BWART_I = '102'
         OR ITAB_NEW-BWART_I = '122' OR ITAB_NEW-BWART_I = '123'.

        QUAN1 = QUAN1 + ITAB_NEW-MENGE_I.
        AMT1 = AMT1 + ITAB_NEW-DMBTR_I.
        MOVE ITAB_NEW-MEINS_I TO IT_FINAL1-MEINS1.

      ENDIF.
      IF ITAB_NEW-BWART_I = '201' OR ITAB_NEW-BWART_I = '202'
         OR ITAB_NEW-BWART_I = '261' OR ITAB_NEW-BWART_I = '262'.

        QUAN2 = QUAN2 + ITAB_NEW-MENGE_I.
        AMT2 = AMT2 + ITAB_NEW-DMBTR_I.
        MOVE ITAB_NEW-MEINS_I TO IT_FINAL1-MEINS2.

      ENDIF.
    ENDLOOP.
      MOVE QUAN1 TO IT_FINAL1-MENGE1.
      MOVE QUAN2 TO IT_FINAL1-MENGE2.
      MOVE AMT1 TO IT_FINAL1-DMBTR1.
      MOVE AMT2 TO IT_FINAL1-DMBTR2.
      APPEND IT_FINAL1.
      CLEAR: QUAN1,QUAN2,
      AMT1,AMT2,IT_FINAL.
     ENDLOOP.
 
 LOOP AT IT_FINAL1.
    MOVE-CORRESPONDING IT_FINAL1 TO ZTPLSTOCK.
    INSERT ZTPLSTOCK.
  ENDLOOP.

In the above program i need to insert month fields in zmonth field in the table.

in the selection-screen s_month high and low values are there how to insert those values.

suggest some ideas.

regards,

Bathri