连接SQL,操作数据库实例(二)

*&---------------------------------------------------------------------*
*& Report  ZISD0065                                                    *
*&                                                                     *
*&---------------------------------------------------------------------*
*& Author:   vista                                                     *
*& Date  :   2008-10-27
*& Request:  "DEVK925889                                               *
*&---------------------------------------------------------------------*

REPORT  ZISD0065                                .

TYPE-POOLS: SLIS .

************************************************************************
************************************************************************
*  D E C L A R A T I O N
************************************************************************
************************************************************************

TABLES: VBAK,LIPS,LIKP,MKPF,MSEG.

DATA: BEGIN OF ITAB OCCURS 0,
        WERKS LIKE LIPS-WERKS,                  "Plant
        MATNR LIKE LIPS-MATNR,                  "Material No
        VBELN LIKE LIPS-VBELN,                  "DN
        POSNR LIKE LIPS-POSNR,                  "DN Item
        KUNNR LIKE LIKP-KUNNR,                  "Ship to party
*        LFIMG LIKE LIPS-LFIMG,                  "Delivery qty
        KCMENG LIKE LIPS-KCMENG,                 "Delivery qty by item
*        DNQTY(13) TYPE P DECIMALS 3,            "Total DN Qty
        WADAT_IST  LIKE LIKP-WADAT_IST,         "Ship Out Date
        CPUTM LIKE MKPF-CPUTM,                  "Ship Time
      END OF ITAB.


DATA: BEGIN OF DTAB OCCURS 0,
        WERKS LIKE LIPS-WERKS,                  "Plant
        MATNR LIKE LIPS-MATNR,                  "Material No
        VBELN LIKE LIPS-VBELN,                  "DN
        POSNR LIKE LIPS-POSNR,                  "DN Item
        KUNNR LIKE LIKP-KUNNR,                  "Ship to party
*        LFIMG LIKE LIPS-LFIMG,                  "Delivery qty
        KCMENG LIKE LIPS-KCMENG,                 "Delivery qty by item
*        DNQTY(15) TYPE P DECIMALS 3,            "Total DN Qty
        WADAT_IST  LIKE LIKP-WADAT_IST,         "Ship Out Date
        CPUTM LIKE MKPF-CPUTM,                  "Ship Time
      END OF DTAB.

DATA: BEGIN OF KTAB OCCURS 0,
        WERKS LIKE LIPS-WERKS,                  "Plant
        MATNR LIKE LIPS-MATNR,                  "Material No
        VBELN LIKE LIPS-VBELN,                  "DN
        POSNR LIKE LIPS-POSNR,                  "DN Item
        KUNNR LIKE LIKP-KUNNR,                  "Ship to party
*        LFIMG LIKE LIPS-LFIMG,                  "Delivery qty
        KCMENG LIKE LIPS-KCMENG,                "Delivery qty by item
*        DNQTY(15) TYPE P DECIMALS 3,            "Total DN Qty
        WADAT_IST LIKE LIKP-WADAT_IST,         "Ship Out Date
        CPUTM LIKE MKPF-CPUTM,                  "Ship Time
        AUART LIKE VBAK-AUART,                  "SO Type
      END OF KTAB.

DATA: BEGIN OF TTAB OCCURS 0,
        MENGE LIKE MSEG-MENGE,
        WERKS LIKE MSEG-WERKS,
        BWART LIKE MSEG-BWART,
        LGORT LIKE MSEG-LGORT,
        SHKZG LIKE MSEG-SHKZG,
        BUDAT LIKE MKPF-BUDAT,
      END OF TTAB.


DATA: BEGIN OF TIBL OCCURS 0,
        PO_REL TYPE I,
        WIP TYPE I,
        REJECT TYPE I,
        SPECIAL_OPEN TYPE I,
        SPECIAL_CLOSE TYPE I,
        IBL_SHIP_TO_VW TYPE I,
        IBL_SHIP_TO_OTH TYPE I,
        IBL_VW_SHIP_OUT TYPE I,
        SAP_SHIP_TO_VW TYPE I,
        SAP_SHIP_TO_OTH TYPE I,
        SAP_VW_SHIP_OUT TYPE I,
        FG TYPE I,
        VW_MOVE_OUT TYPE I,
        FROM_DATE(20),
        TO_DATE(20),
        CUR_DATE(20),
      END OF TIBL.


DATA: BEGIN OF TAB1 OCCURS 0,
        CURR_DATE(10) TYPE C,
        DNQTY1 TYPE I,
        DNQTY2 TYPE I,
        DNQTY3 TYPE I,
        DNQTY4 TYPE I,
      END OF TAB1.


DATA: DNQTY1(15) TYPE P DECIMALS 3,
      DNQTY2(15) TYPE P DECIMALS 3,
      DNQTY3(15) TYPE P DECIMALS 3,
      DNQTY4(15) TYPE P DECIMALS 3.

DATA: FMDATE(12) TYPE C,
      ENDATE(12) TYPE C.

DATA: WA_TAB1 LIKE TAB1.

*&---------------------------------------------------------------------*
*&                      S E L E C T I O N  S C R E E N                 *
*&---------------------------------------------------------------------*

SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-000.
SELECT-OPTIONS: S_DATE FOR LIKP-WADAT_IST,
                S_TIME FOR MKPF-CPUTM.
SELECTION-SCREEN END OF BLOCK B1.


*----------------------------------------------------------------------*
*  Macro Definition                                                    *
*----------------------------------------------------------------------*
*  Get SQL Query result
DEFINE IBL_FETCH_DATA.

  TRY.
      EXEC SQL.
        FETCH NEXT &3 INTO :&1
      ENDEXEC.

    CATCH CX_SY_NATIVE_SQL_ERROR.
      &2 = '99'.
  ENDTRY.

  IF &2 NE 99.
    &2 = SY-SUBRC.
  ENDIF.

END-OF-DEFINITION.

*  Close SQL Cursor
DEFINE IBL_CLOSE_CURSOR.

  TRY.
      EXEC SQL.
        CLOSE &1
      ENDEXEC.

    CATCH CX_SY_NATIVE_SQL_ERROR.
      &2 = '99'.
  ENDTRY.

  IF &2 NE 99.
    &2 = SY-SUBRC.
  ENDIF.

END-OF-DEFINITION.


*----------------------------------------------------------------------*
*        INITIALIZATION                                                *
*----------------------------------------------------------------------*

INITIALIZATION.

*----------------------------------------------------------------------*
*        START-OF-SELECTION                                            *
*----------------------------------------------------------------------*
START-OF-SELECTION.

  PERFORM GET_DATA.

*----------------------------------------------------------------------*
*        END-OF-SELECTION                                              *
*----------------------------------------------------------------------


*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM GET_DATA .

**from date
  CONCATENATE  S_DATE-LOW(4) '-' S_DATE-LOW+4(2) '-' S_DATE-LOW+6(2)
              INTO FMDATE.
**end  date
  CONCATENATE  S_DATE-HIGH(4) '-' S_DATE-HIGH+4(2) '-' S_DATE-HIGH+6(2)
                 INTO ENDATE.


*SAP SHIP TO VW QTY
  SELECT SUM( LIPS~KCMENG ) AS KCMENG LIKP~WADAT_IST
*  MKPF~CPUTM LIPS~WERKS LIPS~VBELN LIPS~POSNR LIPS~MATNR
         INTO CORRESPONDING FIELDS OF TABLE ITAB
         FROM LIPS
         INNER JOIN LIKP ON LIKP~VBELN = LIPS~VBELN
         INNER JOIN MKPF ON MKPF~XBLNR = LIPS~VBELN
         WHERE LIKP~KUNNR = '0000088888'
         AND LIKP~WADAT_IST IN S_DATE
         AND MKPF~CPUTM IN S_TIME
         GROUP BY LIKP~WADAT_IST.


*SAP SHIP TO OTH QTY
  SELECT   SUM( LIPS~KCMENG ) AS KCMENG
           LIKP~WADAT_IST
*          MKPF~CPUTM LIPS~WERKS LIPS~VBELN
*          LIPS~POSNR LIPS~MATNR
         INTO CORRESPONDING FIELDS OF TABLE DTAB
         FROM LIPS
         INNER JOIN LIKP ON LIKP~VBELN = LIPS~VBELN
         INNER JOIN MKPF ON MKPF~XBLNR = LIPS~VBELN
         WHERE LIKP~KUNNR <> '0000088888'
         AND LIKP~WADAT_IST IN S_DATE
         AND MKPF~CPUTM IN S_TIME
         GROUP BY LIKP~WADAT_IST.


*SAP VW SHIP OUT QTY
  SELECT SUM( LIPS~KCMENG ) AS KCMENG LIKP~WADAT_IST
*        MKPF~CPUTM LIPS~WERKS LIPS~VBELN
*        LIPS~POSNR LIPS~MATNR VBAK~AUART
         INTO CORRESPONDING FIELDS OF TABLE KTAB
         FROM LIPS
         INNER JOIN LIKP ON LIKP~VBELN = LIPS~VBELN
         INNER JOIN MKPF ON MKPF~XBLNR = LIPS~VBELN
         INNER JOIN VBAK ON VBAK~VBELN = LIPS~KDAUF
         WHERE LIKP~KUNNR <> '0000088888'
         AND VBAK~AUART = 'SHIP'
         AND LIKP~WADAT_IST IN S_DATE
         AND MKPF~CPUTM IN S_TIME
         GROUP BY LIKP~WADAT_IST.


*VW MOVE OUT QTY
  SELECT SUM( MSEG~MENGE ) AS MENGE MKPF~BUDAT
  INTO CORRESPONDING FIELDS OF TABLE TTAB
  FROM MSEG
  INNER JOIN MKPF ON MKPF~MBLNR = MSEG~MBLNR
                  AND MKPF~MJAHR = MSEG~MJAHR
  WHERE MKPF~BUDAT IN S_DATE AND MKPF~CPUTM IN S_TIME
  AND MSEG~BWART = '601'
  AND MSEG~SHKZG = 'H'
  GROUP BY MKPF~BUDAT.


  IF ITAB[] IS NOT INITIAL.
    LOOP AT ITAB.
      TAB1-DNQTY1 = ITAB-KCMENG.
      TAB1-CURR_DATE = ITAB-WADAT_IST.
      APPEND TAB1.
      CLEAR TAB1.
    ENDLOOP.
  ENDIF.

  IF DTAB[] IS NOT INITIAL.
    LOOP AT DTAB.
      TAB1-DNQTY2 = DTAB-KCMENG.
      TAB1-CURR_DATE = DTAB-WADAT_IST.
      APPEND TAB1.
      CLEAR TAB1.
    ENDLOOP.
  ENDIF.

  IF KTAB[] IS NOT INITIAL.
    LOOP AT KTAB.
      TAB1-DNQTY3 = KTAB-KCMENG.
      TAB1-CURR_DATE = KTAB-WADAT_IST.
      APPEND TAB1.
      CLEAR TAB1.
    ENDLOOP.
  ENDIF.

  IF TTAB[] IS NOT INITIAL.
    LOOP AT TTAB.
      TAB1-DNQTY4 = TTAB-MENGE.
      TAB1-CURR_DATE = TTAB-BUDAT.
      APPEND TAB1.
      CLEAR TAB1.
    ENDLOOP.
  ENDIF.

  IF TAB1[] IS NOT INITIAL.
    LOOP AT TAB1.
      READ TABLE ITAB WITH KEY WADAT_IST = TAB1-CURR_DATE.
      IF SY-SUBRC = 0.
        TAB1-DNQTY2 = ITAB-KCMENG.
        TAB1-CURR_DATE = ITAB-WADAT_IST.
      ENDIF.

      READ TABLE DTAB WITH KEY WADAT_IST = TAB1-CURR_DATE.
      IF SY-SUBRC = 0.
        TAB1-DNQTY2 = DTAB-KCMENG.
        TAB1-CURR_DATE = DTAB-WADAT_IST.
      ENDIF.

      READ TABLE KTAB WITH KEY WADAT_IST = TAB1-CURR_DATE.
      IF SY-SUBRC = 0.
        TAB1-DNQTY3 = KTAB-KCMENG.
        TAB1-CURR_DATE = KTAB-WADAT_IST.
      ENDIF.

      READ TABLE TTAB WITH KEY BUDAT = TAB1-CURR_DATE.
      IF SY-SUBRC = 0.
        TAB1-DNQTY4 = TTAB-MENGE.
        TAB1-CURR_DATE = TTAB-BUDAT.
      ENDIF.
      MODIFY TAB1.
      CLEAR TAB1.

    ENDLOOP.
  ENDIF.


*Insert/Update data to IBL Table

  PERFORM CATCH_IBL_DATA.


ENDFORM.                    " GET_DATA
*

*&---------------------------------------------------------------------*
*&      Form  CHECK_DBCON
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->ITAB-WERKS text
*      <--P_DBCONNAME text
*      <--P_SUBRC  text
*----------------------------------------------------------------------*
FORM CHECK_DBCON USING  P_DBCONNAME
                        P_SUBRC.
  TABLES:DBCON.
  CLEAR DBCON.
  SELECT SINGLE *  FROM  DBCON
         WHERE  CON_NAME  = P_DBCONNAME.
  IF SY-SUBRC NE 0.
    MESSAGE E398(00)
      WITH 'DB Connection ' P_DBCONNAME
           'has not been set up.'
           'Please check table DBCON.'.
    P_SUBRC = SY-SUBRC.
    CLEAR P_DBCONNAME.
  ELSE.
    P_SUBRC = SY-SUBRC.
  ENDIF.
ENDFORM.                    "CHECK_DBCON


*&---------------------------------------------------------------------*
*&      Form  CONNECT_DBCON
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_DBCONNAME  text
*      <--P_SUBRC  text
*----------------------------------------------------------------------*
FORM CONNECT_DBCON USING P_DBCONNAME LIKE DBCON-CON_NAME
                   CHANGING P_SUBRC.
  TRY.
      EXEC SQL.
        CONNECT TO :P_DBCONNAME
      ENDEXEC.
      EXEC SQL.
        SET CONNECTION :P_DBCONNAME
      ENDEXEC.

    CATCH CX_SY_NATIVE_SQL_ERROR.
      P_SUBRC = '99'.
  ENDTRY.
ENDFORM.                    " connect_dbcon

*&---------------------------------------------------------------------*
*&      Form  DISCONNECT_DBCON
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_L_DBCONNAME  text
*      <--P_L_SUBRC  text
*----------------------------------------------------------------------*
FORM DISCONNECT_DBCON USING P_DBCONNAME  LIKE DBCON-CON_NAME
                      CHANGING P_SUBRC.
  TRY.
      EXEC SQL.
        DISCONNECT :P_DBCONNAME
      ENDEXEC.

    CATCH CX_SY_NATIVE_SQL_ERROR.
      P_SUBRC = '99'.
  ENDTRY.

ENDFORM.                    " connect_dbcon


*&---------------------------------------------------------------------*
*&      Form  CATCH_IBL_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM CATCH_IBL_DATA .
*****Check data if exists in IBL
  DATA: L_DBCONNAME     LIKE DBCON-CON_NAME.
  DATA: L_SUBRC         LIKE SY-SUBRC.
  DATA: L_DBOPEN        LIKE SY-SUBRC.
  DATA: L_DBCON(10) VALUE 'HKMIBL_DG'.

  MOVE L_DBCON TO L_DBCONNAME.

  PERFORM CHECK_DBCON USING  L_DBCONNAME L_SUBRC.

  PERFORM CONNECT_DBCON USING  L_DBCONNAME
                      CHANGING L_SUBRC.

  L_DBOPEN = L_SUBRC.

  IF L_DBOPEN = 0.

    CLEAR L_SUBRC.

    LOOP AT TAB1 INTO WA_TAB1.

      PERFORM PROCESS_DATA CHANGING L_SUBRC.

      DO.
        CLEAR TIBL.
        CLEAR TIBL[].
        IF L_SUBRC = 0.
          IBL_FETCH_DATA TIBL L_SUBRC C11A.
          APPEND TIBL.
        ELSE.
          EXIT.
        ENDIF.
      ENDDO.

      IBL_CLOSE_CURSOR C11A L_SUBRC.

      IF TIBL[] IS NOT INITIAL.
        PERFORM UPDATE_DATA CHANGING L_SUBRC.
      ELSE.
        PERFORM INSERT_DATA CHANGING L_SUBRC.
      ENDIF.

      REFRESH TIBL[].

    ENDLOOP.

    PERFORM DISCONNECT_DBCON USING L_DBCONNAME
                             CHANGING L_SUBRC.
  ENDIF.
ENDFORM.                    " CATCH_IBL_DATA


*&---------------------------------------------------------------------*
*&      Form  PROCESS_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_TDATE  text
*      <--P_L_SUBRC  text
*----------------------------------------------------------------------*
FORM PROCESS_DATA USING L_SUBRC.

  CLEAR L_SUBRC.
  TRY.
      EXEC SQL.

        OPEN C11A FOR
        SELECT * FROM CHKWIPQTY
               where CUR_DATE = :WA_TAB1-CURR_DATE.
      ENDEXEC.

      L_SUBRC = SY-SUBRC.

    CATCH CX_SY_NATIVE_SQL_ERROR.
      L_SUBRC = '99'.
  ENDTRY.
ENDFORM.                    " PROCESS_DATA


*&---------------------------------------------------------------------*
*&      Form  UPDATE_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM UPDATE_DATA USING L_SUBRC.

  CLEAR L_SUBRC.
  TRY.
      EXEC SQL.

        update ChkWipQty set (sap_ship_to_vw,sap_ship_to_oth,
                   sap_vw_ship_out,vw_move_out,from_date,to_date)
        values( :WA_TAB1-DNQTY1, :WA_TAB1-DNQTY2, :WA_TAB1-DNQTY3,
                :WA_TAB1-DNQTY4, :FMDATE, :ENDATE )
                     where CUR_DATE = :WA_TAB1-CURR_DATE.
      ENDEXEC.

      COMMIT WORK.

      L_SUBRC = SY-SUBRC.

    CATCH CX_SY_NATIVE_SQL_ERROR.
      L_SUBRC = '99'.
  ENDTRY.

  IF L_SUBRC = 0.
    MESSAGE S000(00) WITH: 'Update Data to IBL table succeed!'.
  ELSE.
    MESSAGE E000(00) WITH: 'Update Data to IBL table failed!'.
  ENDIF.

ENDFORM.                    " UPDATE_DATA


*&---------------------------------------------------------------------*
*&      Form  INSERT_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM INSERT_DATA USING L_SUBRC.
  CLEAR L_SUBRC.
  TRY.
      EXEC SQL.
        insert ChkWipQty(sap_ship_to_vw,sap_ship_to_oth,
        sap_vw_ship_out,vw_move_out,from_date,to_date,cur_date)
        values( :WA_TAB1-DNQTY1, :WA_TAB1-DNQTY2, :WA_TAB1-DNQTY3,
                :WA_TAB1-DNQTY4, :FMDATE, :ENDATE, :WA_TAB1-CURR_DATE )
      ENDEXEC.

      COMMIT WORK.

      L_SUBRC = SY-SUBRC.

    CATCH CX_SY_NATIVE_SQL_ERROR.
      L_SUBRC = '99'.
  ENDTRY.

  IF L_SUBRC = 0.
    MESSAGE S000(00) WITH: 'Insert Data to IBL table succeed!'.
  ELSE.
    MESSAGE E000(00) WITH: 'Insert Data to IBL table failed!'.
  ENDIF.

ENDFORM.                    " INSERT_DATA 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值