美文网首页
SQL SERVER作业Interface

SQL SERVER作业Interface

作者: a9b854aded01 | 来源:发表于2017-09-12 10:02 被阅读0次

    用于SQL SERVER 数据库 两个系统的数据库之间的信息传递

    
    USE [local]
    GO
    /****** Object:  StoredProcedure [dbo].[USP_WMS_PM270]    Script Date: 2017-07-13 8:51:37 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    /*==============================================================================================================================================
    프로그램ID : DBO.[USP_WMS_PM270]
    업  무  명 : 입고, 반입대기 Interface (ERP → WMS)
    최초작성일 : 2013/06/18
    최종작성일 : 2013/07/01
    개  발  자 : 구성호
    수  정  자 : 구성호
    변경  이력 : 2013.07.01 GSH : 관리번호 생성체계 수정
                 2013.07.12 GSH : --I/F시 Today로 변경 : SK90T.BUDAT-> GEDATE() ->SK90T.BUDAT 
                 2013.07.17 GSH : 311,325자재예약관리에서 처리로 인한 입고대기I/F대상 추가   
                 
    입  력  값 : 회사코드, 공장
                 EXEC  [dbo].[USP_WMS_PM270] '2000','2000'
    출  력  값 : Table 
    ==============================================================================================================================================*/
    ALTER PROCEDURE [dbo].[USP_WMS_PM270]   
        @S_BUKRS            NVARCHAR(4),  --회사코드
        @S_WERKS            NVARCHAR(4)   --공장
    AS
    BEGIN
        SET XACT_ABORT ON
        
        DECLARE @V_EMAIL_WMS    NVARCHAR(MAX) = 'lvwenting@dy.co.kr;gaowg@dy.co.kr'
        DECLARE @V_EMAIL_IT     NVARCHAR(MAX) = ''
        DECLARE @V_SUBJECT      NVARCHAR(MAX)  = N'(DEV)'+N'入库, 返入待机'+N' Interface (ERP → WMS) Error'
        DECLARE @V_CONTENTS     NVARCHAR(MAX)  = ''
                
        BEGIN TRY   
            BEGIN DISTRIBUTED TRANSACTION
            
            DECLARE @V_PRE_DAY INT = -1
    
            --BEGIN - 임시
    
            --SELECT 'ERP LOG',* FROM dbo.WMS_PM270
            --SELECT * FROM [DY_WMS].[dbo].[CW_PM270]
            --DELETE FROM [DY_WMS].[dbo].[CW_PM270]
            --DELETE FROM dbo.WMS_PM270
    
            --END - 임시
    
            CREATE TABLE #T_PM270
            (   
                BUKRS       NVARCHAR(4)         COLLATE Korean_Wansung_CI_AS,
                INSEQ       NVARCHAR(10)        COLLATE Korean_Wansung_CI_AS,
                MBLNR       NVARCHAR(10)        COLLATE Korean_Wansung_CI_AS,
                MJAHR       NVARCHAR(4)         COLLATE Korean_Wansung_CI_AS,
                ZEILE       INT,
                MATCOD      NVARCHAR(18)        COLLATE Korean_Wansung_CI_AS,
                EBELN       NVARCHAR(10)        COLLATE Korean_Wansung_CI_AS,
                EBELP       INT,
                RSNUM       NVARCHAR(10)        COLLATE Korean_Wansung_CI_AS,
                RSPOS       INT,
                INNO        NVARCHAR(18)        COLLATE Korean_Wansung_CI_AS,
                INDAY       DATETIME,
                PLTDIV      NVARCHAR(2)         COLLATE Korean_Wansung_CI_AS,
                EPTDIV      NVARCHAR(1)         COLLATE Korean_Wansung_CI_AS,
                WERKS       NVARCHAR(4)         COLLATE Korean_Wansung_CI_AS,
                LGORT       NVARCHAR(4)         COLLATE Korean_Wansung_CI_AS,
                LIFNR       NVARCHAR(10)        COLLATE Korean_Wansung_CI_AS,
                NAME1       NVARCHAR(35)        COLLATE Korean_Wansung_CI_AS,
                BWART       NVARCHAR(3)         COLLATE Korean_Wansung_CI_AS,
                MTART       NVARCHAR(4)         COLLATE Korean_Wansung_CI_AS,
                SOBSL       NVARCHAR(2)         COLLATE Korean_Wansung_CI_AS,
                LAND1       NVARCHAR(3)         COLLATE Korean_Wansung_CI_AS,
                INOKQTY     DECIMAL(13, 3),
                ENDQTY      DECIMAL(13, 3),
                PRSDIV      NVARCHAR(2)         COLLATE Korean_Wansung_CI_AS,
                IEDIV       NVARCHAR(1)         COLLATE Korean_Wansung_CI_AS,
                RTDIV       NVARCHAR(1)         COLLATE Korean_Wansung_CI_AS,
                DELDIV      NVARCHAR(1)         COLLATE Korean_Wansung_CI_AS,
                USNAM       NVARCHAR(10)        COLLATE Korean_Wansung_CI_AS,
                SYSDAY      DATETIME    
            )
    
            INSERT INTO #T_PM270 
                  (BUKRS,INSEQ,MBLNR,MJAHR,ZEILE,
                   MATCOD,
                   EBELN,EBELP,RSNUM,RSPOS,
                   INNO,INDAY,
                   PLTDIV,EPTDIV,
                   WERKS,LGORT,LIFNR,
                   NAME1,
                   BWART,MTART,SOBSL,LAND1,
                   INOKQTY,ENDQTY,
                   PRSDIV,IEDIV,
                   RTDIV,DELDIV,
                   USNAM,SYSDAY)
            SELECT TMP_DATA.BUKRS,
                   SUBSTRING(CONVERT(NVARCHAR(11),10000000000+((ROW_NUMBER() OVER(ORDER BY TMP_DATA.BUKRS,TMP_DATA.WERKS,TMP_DATA.MBLNR,TMP_DATA.MJAHR,TMP_DATA.ZEILE,TMP_DATA.RSNUM,TMP_DATA.RSPOS,TMP_DATA.MATCOD DESC))+ISNULL((SELECT MAX(CONVERT(DECIMAL(10,0),ISNULL(Z.INSEQ,0))) FROM  [DY_WMS].[dbo].[CW_PM270] Z ),0))),2,10) AS INSEQ,
                   TMP_DATA.MBLNR,TMP_DATA.MJAHR,TMP_DATA.ZEILE,
                   TMP_DATA.MATCOD,
                   TMP_DATA.EBELN,TMP_DATA.EBELP,TMP_DATA.RSNUM,TMP_DATA.RSPOS,
                   TMP_DATA.INNO,TMP_DATA.INDAY,
                   TMP_DATA.PLTDIV,TMP_DATA.EPTDIV,
                   TMP_DATA.WERKS,TMP_DATA.LGPRO,TMP_DATA.LIFNR,
                   TMP_DATA.NAME1,
                   TMP_DATA.BWART,TMP_DATA.MTART,TMP_DATA.SOBSL,TMP_DATA.LAND1,
                   TMP_DATA.INOKQTY, TMP_DATA.ENDQTY,
                   TMP_DATA.PRSDIV, TMP_DATA.IEDIV,
                   TMP_DATA.RTDIV, TMP_DATA.DELDIV,
                   TMP_DATA.USNAM,TMP_DATA.SYSDAY
            FROM (
            SELECT SK90T.BUKRS, SK90T.MBLNR,SK90T.MJAHR,SK90T.ZEILE,
                   SK90T.MATNR AS MATCOD,       
                   ISNULL(SK90T.EBELN,'') AS EBELN,
                   ISNULL(SK90T.EBELP,0) AS EBELP,
                   '' AS RSNUM,0 AS RSPOS,
                   ISNULL(MM31T.TRNNO,'') AS INNO,             
                   SK90T.BUDAT AS INDAY, --I/F시 Today로 변경  : SK90T.BUDAT-> GEDATE() ->SK90T.BUDAT
                   SUBSTRING(SK90T.WERKS,1,2) AS  PLTDIV,SUBSTRING(SK90T.WERKS,2,1) AS EPTDIV,
                   SK90T.WERKS,SK90T.LGPRO,SK90T.LIFNR1 AS LIFNR,
                   CONVERT(NVARCHAR(35),FC20M.CSTNM) AS NAME1,
                   SK90T.BWART,AD10M.MTART,AD10M.SOBSL,ISNULL(FC20M.CNTRY,'') AS LAND1,
                   CONVERT(DECIMAL(13,3),ISNULL(SK90T.MENGE,0)) AS INOKQTY,0.000 AS ENDQTY,
                   '10' AS PRSDIV,      
                   ISNULL(FC20M.DFDIV,'') AS IEDIV,
                    '1' AS RTDIV,
                   'N' AS DELDIV,
                   ISNULL(SK90T.AENAM,'') AS USNAM,GETDATE() as SYSDAY
            FROM SK90T
                 INNER JOIN AD10M
                 ON AD10M.BUKRS = SK90T.BUKRS
                    AND AD10M.WERKS = SK90T.WERKS
                    AND AD10M.MATNR = SK90T.MATNR
                    AND AD10M.LOVEM = 'N'   
                 INNER JOIN MM31T
                 ON MM31T.BUKRS = SK90T.BUKRS
                    AND MM31T.WERKS = SK90T.WERKS
                    AND MM31T.MATNR = SK90T.MATNR
                    AND MM31T.DELNO = SK90T.DELNO
                    AND MM31T.DELNP = SK90T.DELNP
                    AND MM31T.QMINT != '10' 
                    AND MM31T.ASSIN = 'N'
                 INNER JOIN MM30T
                 ON MM30T.BUKRS = MM31T.BUKRS
                    AND MM30T.WERKS = MM31T.WERKS
                    AND MM30T.DELNO = MM31T.DELNO 
                    AND MM30T.LOVEM = 'N'    
                 INNER JOIN FC20M
                 ON FC20M.BUKRS = SK90T.BUKRS
                    AND FC20M.CSTNO = SK90T.LIFNR1
                    AND FC20M.LOVEM = 'N'   
            WHERE SK90T.BUKRS = @S_BUKRS
              AND SK90T.WERKS = @S_WERKS
              AND (SK90T.EBELN LIKE '45%' OR SK90T.EBELN LIKE '47%')
              AND SK90T.BWART = '101'
              AND SK90T.SHKZG = 'S'
              AND SK90T.LOVEM = 'N'
              AND SK90T.LAEDA BETWEEN DATEADD(DAY,@V_PRE_DAY,GETDATE()) AND GETDATE()
              AND SK90T.LGPRO IN (SELECT Z.CodeValue 
                                    FROM FC01M Z
                                   WHERE Z.BUKRS = @S_BUKRS
                                     and Z.Codeid = 'AD03' 
                                     and Z.uddec1 = CONVERT(DECIMAL(13,3),@S_WERKS))
            UNION ALL
            (
            SELECT SK90T.BUKRS, SK90T.MBLNR,SK90T.MJAHR,SK90T.ZEILE,
                   SK90T.MATNR AS MATCOD,       
                   ISNULL(SK90T.EBELN,'') AS EBELN,
                   ISNULL(SK90T.EBELP,0) AS EBELP,
                   '' AS RSNUM,0 AS RSPOS,
                   ISNULL(MM31T.TRNNO,'') AS INNO, 
                   SK90T.BUDAT AS INDAY, --I/F시 Today로 변경 : SK90T.BUDAT-> GEDATE() ->SK90T.BUDAT
                   SUBSTRING(SK90T.WERKS,1,2) AS  PLTDIV,SUBSTRING(SK90T.WERKS,2,1) AS EPTDIV,
                   SK90T.WERKS,SK90T.LGPRO,SK90T.LIFNR1 AS LIFNR,
                   CONVERT(NVARCHAR(35),FC20M.CSTNM) AS NAME1,
                   SK90T.BWART,AD10M.MTART,AD10M.SOBSL,ISNULL(FC20M.CNTRY,'') AS LAND1,
                   CONVERT(DECIMAL(13,3),ISNULL(SK90T.MENGE,0)) AS INOKQTY,0.000 AS ENDQTY,
                   '10' AS PRSDIV,      
                   ISNULL(FC20M.DFDIV,'') AS IEDIV,
                    '1' AS RTDIV,
                   'N' AS DELDIV,
                   ISNULL(SK90T.AENAM,'') AS USNAM,GETDATE() as SYSDAY
            FROM SK90T
                 INNER JOIN AD10M
                 ON AD10M.BUKRS = SK90T.BUKRS
                    AND AD10M.WERKS = SK90T.WERKS
                    AND AD10M.MATNR = SK90T.MATNR
                    AND AD10M.LOVEM = 'N'   
                 INNER JOIN MM31T
                 ON MM31T.BUKRS = SK90T.BUKRS
                    AND MM31T.WERKS = SK90T.WERKS
                    AND MM31T.MATNR = SK90T.MATNR
                    AND MM31T.DELNO = SK90T.DELNO
                    AND MM31T.DELNP = SK90T.DELNP
                    AND (MM31T.QMINT = '10' OR (MM31T.QMINT = '20' AND MM31T.ASSIN = 'Y')) 
                    AND MM31T.INSTS = 'Y'
                 INNER JOIN MM30T
                 ON MM30T.BUKRS = MM31T.BUKRS
                    AND MM30T.WERKS = MM31T.WERKS
                    AND MM30T.DELNO = MM31T.DELNO 
                    AND MM30T.LOVEM = 'N'    
                 INNER JOIN FC20M
                 ON FC20M.BUKRS = SK90T.BUKRS
                    AND FC20M.CSTNO = SK90T.LIFNR1
                    AND FC20M.LOVEM = 'N'   
            WHERE SK90T.BUKRS = @S_BUKRS
              AND SK90T.WERKS = @S_WERKS
              AND (SK90T.EBELN LIKE '45%' OR SK90T.EBELN LIKE '47%')
              AND SK90T.BWART = '321'  
              AND SK90T.SHKZG = 'S'
              AND SK90T.LOVEM = 'N'
              AND SK90T.LAEDA BETWEEN DATEADD(DAY,@V_PRE_DAY,GETDATE()) AND GETDATE()
              AND SK90T.LGPRO IN (SELECT Z.CodeValue 
                                    FROM FC01M Z
                                   WHERE Z.BUKRS = @S_BUKRS
                                     and Z.Codeid = 'AD03' 
                                     and Z.uddec1 = CONVERT(DECIMAL(13,3),@S_WERKS))
            )
            UNION ALL
            (
            SELECT SK90T.BUKRS, SK90T.MBLNR,SK90T.MJAHR,SK90T.ZEILE,
                   SK90T.MATNR AS MATCOD,       
                   ISNULL(SK90T.EBELN,'') AS EBELN,
                   ISNULL(SK90T.EBELP,0) AS EBELP,
                   '' AS RSNUM,0 AS RSPOS,
                   ISNULL(MM31T.TRNNO,'') AS INNO, 
                   SK90T.BUDAT AS INDAY, --I/F시 Today로 변경 : SK90T.BUDAT-> GEDATE() ->SK90T.BUDAT
                   SUBSTRING(SK90T.WERKS,1,2) AS  PLTDIV,SUBSTRING(SK90T.WERKS,2,1) AS EPTDIV,
                   SK90T.WERKS,SK90T.LGPRO,SK90T.LIFNR1 AS LIFNR,
                   CONVERT(NVARCHAR(35),FC20M.CSTNM) AS NAME1,
                   SK90T.BWART,AD10M.MTART,AD10M.SOBSL,ISNULL(FC20M.CNTRY,'') AS LAND1,
                   CONVERT(DECIMAL(13,3),ISNULL(SK90T.MENGE,0)) AS INOKQTY,0.000 AS ENDQTY,
                   '10' AS PRSDIV,      
                   ISNULL(FC20M.DFDIV,'') AS IEDIV,
                    '1' AS RTDIV,
                   'N' AS DELDIV,
                   ISNULL(SK90T.AENAM,'') AS USNAM,GETDATE() as SYSDAY
            FROM SK90T
                 INNER JOIN AD10M
                 ON AD10M.BUKRS = SK90T.BUKRS
                    AND AD10M.WERKS = SK90T.WERKS
                    AND AD10M.MATNR = SK90T.MATNR
                    AND AD10M.LOVEM = 'N'   
                 INNER JOIN MM31T
                 ON MM31T.BUKRS = SK90T.BUKRS
                    AND MM31T.WERKS = SK90T.WERKS
                    AND MM31T.MATNR = SK90T.MATNR
                    AND MM31T.DELNO = SK90T.DELNO
                    AND MM31T.DELNP = SK90T.DELNP
                    AND MM31T.MVIND = 'Y'
                 INNER JOIN MM30T
                 ON MM30T.BUKRS = MM31T.BUKRS
                    AND MM30T.WERKS = MM31T.WERKS
                    AND MM30T.DELNO = MM31T.DELNO 
                    AND MM30T.LOVEM = 'N'    
                 INNER JOIN FC20M
                 ON FC20M.BUKRS = SK90T.BUKRS
                    AND FC20M.CSTNO = SK90T.LIFNR1
                    AND FC20M.LOVEM = 'N'   
            WHERE SK90T.BUKRS = @S_BUKRS
              AND SK90T.WERKS = @S_WERKS
              AND (SK90T.EBELN LIKE '45%' OR SK90T.EBELN LIKE '47%')
              AND SK90T.BWART = '311'  
              AND SK90T.SHKZG = 'S'
              AND SK90T.LOVEM = 'N'
              AND SK90T.LAEDA BETWEEN DATEADD(DAY,@V_PRE_DAY,GETDATE()) AND GETDATE()
              AND SK90T.LGPRO IN (SELECT Z.CodeValue 
                                    FROM FC01M Z
                                   WHERE Z.BUKRS = @S_BUKRS
                                     and Z.Codeid = 'AD03' 
                                     and Z.uddec1 = CONVERT(DECIMAL(13,3),@S_WERKS))
            )
            UNION ALL
            (
            SELECT SK90T.BUKRS, SK90T.MBLNR,SK90T.MJAHR,SK90T.ZEILE,
                   SK90T.MATNR AS MATCOD,
                   '' AS EBELN,0 AS EBELP,
                   '' AS RSNUM,0 AS RSPOS,
                   '' AS INNO,
                   SK90T.BUDAT AS INDAY, --I/F시 Today로 변경 : SK90T.BUDAT-> GEDATE() ->SK90T.BUDAT
                   SUBSTRING(SK90T.WERKS,1,2) AS  PLTDIV,SUBSTRING(SK90T.WERKS,2,1) AS EPTDIV,
                   SK90T.WERKS,SK90T.LGPRO,(CASE WHEN SK90T.BWART = 'Y01' AND SK90T.SHKZG = 'H' THEN SK90T.KUNNR 
                                                 WHEN SK90T.BWART IN ('021','023','025','031','033','035','037','041','051','061') AND SK90T.SHKZG = 'H' 
                                                 THEN (SELECT TOP 1 Z.KOSTL FROM PP30T Z WHERE Z.RSNUM = SK90T.RSNUM AND Z.RSPOS = SK90T.RSPOS)
                                                 WHEN SK90T.BWART NOT IN ('Y01','021','023','025','031','033','035','037','041','051','061') AND SK90T.SHKZG = 'S' 
                                                 THEN SK90T.LGPRO ELSE '' END ) AS LIFNR,
                   CONVERT(NVARCHAR(35), (CASE WHEN SK90T.BWART = 'Y01' AND SK90T.SHKZG = 'H' THEN dbo.FN_GET_CSTNM(SK90T.BUKRS,SK90T.KUNNR) 
                                              WHEN SK90T.BWART IN ('021','023','025','031','033','035','037','041','051','061') AND SK90T.SHKZG = 'H' 
                                              THEN (SELECT TOP 1 Z.KSLNM FROM PP30T Z WHERE Z.RSNUM = SK90T.RSNUM AND Z.RSPOS = SK90T.RSPOS)
                                              WHEN SK90T.BWART NOT IN ('Y01','021','023','025','031','033','035','037','041','051','061') AND SK90T.SHKZG = 'S' 
                                              THEN (SELECT TOP 1 description FROM FC01M WHERE BUKRS = SK90T.BUKRS and Codeid = 'AD03' AND LTRIM(RTRIM(CodeValue)) = SK90T.LGPRO) 
                                              ELSE '' END )) AS NAME1,
                   SK90T.BWART,AD10M.MTART,AD10M.SOBSL,
                   (CASE WHEN SK90T.BWART = 'Y01' THEN (SELECT TOP 1 CNTRY FROM FC20M WHERE BUKRS = SK90T.BUKRS and CSTNO = SK90T.KUNNR)
                         ELSE 'CN' END )AS LAND1,
                   CONVERT(DECIMAL(13,3),ISNULL(SK90T.MENGE,0)) AS INOKQTY,0.000 AS ENDQTY,
                   '10' AS PRSDIV,      
                   (CASE WHEN SK90T.BWART = 'Y01' THEN (SELECT TOP 1 DFDIV FROM FC20M WHERE BUKRS = SK90T.BUKRS and CSTNO = SK90T.KUNNR)
                         ELSE '1' END ) AS IEDIV,
                   '2' AS RTDIV,
                   'N' AS DELDIV,
                   ISNULL(SK90T.AENAM,'') AS USNAM,GETDATE() as SYSDAY
            FROM SK90T            
                 INNER JOIN AD10M
                 ON AD10M.BUKRS = SK90T.BUKRS
                    AND AD10M.WERKS = SK90T.WERKS
                    AND AD10M.MATNR = SK90T.MATNR
                    AND AD10M.LOVEM = 'N'      
            WHERE SK90T.BUKRS = @S_BUKRS
              AND SK90T.WERKS = @S_WERKS
              AND SK90T.BWART IN ('311','Z11','Z13','Z15','Y01','325',
                                  '021','023','025','031','033','035','037','041','051','061')
              AND SK90T.LOVEM = 'Y'  
              AND SK90T.SHKZG = 'H'
              AND SK90T.LAEDA BETWEEN DATEADD(DAY,@V_PRE_DAY,GETDATE()) AND GETDATE()
              AND SK90T.LGPRO IN (SELECT Z.CodeValue 
                                    FROM FC01M Z
                                   WHERE Z.BUKRS = @S_BUKRS
                                     and Z.Codeid = 'AD03' 
                                     and Z.uddec1 = CONVERT(DECIMAL(13,3),@S_WERKS)) 
            )                
            ) TMP_DATA
    
            INSERT INTO  [DY_WMS].[dbo].[CW_PM270]
                  (BUKRS,INSEQ,MBLNR,MJAHR,ZEILE,
                   MATCOD,
                   EBELN,EBELP,RSNUM,RSPOS,
                   INNO,INDAY,
                   PLTDIV,EPTDIV,
                   WERKS,LGORT,LIFNR,
                   NAME1,
                   BWART,MTART,SOBSL,LAND1,
                   INOKQTY,ENDQTY,
                   PRSDIV,IEDIV,
                   RTDIV,DELDIV,
                   USNAM,SYSDAY)
            SELECT TMP_DATA.BUKRS,TMP_DATA.INSEQ,TMP_DATA.MBLNR,TMP_DATA.MJAHR,TMP_DATA.ZEILE,
                   TMP_DATA.MATCOD,
                   TMP_DATA.EBELN,TMP_DATA.EBELP,TMP_DATA.RSNUM,TMP_DATA.RSPOS,
                   TMP_DATA.INNO,TMP_DATA.INDAY,
                   TMP_DATA.PLTDIV,TMP_DATA.EPTDIV,
                   TMP_DATA.WERKS,TMP_DATA.LGORT,TMP_DATA.LIFNR,
                   TMP_DATA.NAME1,
                   TMP_DATA.BWART,TMP_DATA.MTART,TMP_DATA.SOBSL,TMP_DATA.LAND1,
                   TMP_DATA.INOKQTY,TMP_DATA.ENDQTY,
                   TMP_DATA.PRSDIV,TMP_DATA.IEDIV,
                   TMP_DATA.RTDIV,TMP_DATA.DELDIV,
                   TMP_DATA.USNAM,TMP_DATA.SYSDAY
             FROM #T_PM270 TMP_DATA   
             WHERE  NOT EXISTS(SELECT Z.MBLNR 
                                FROM  [DY_WMS].[dbo].[CW_PM270] Z
                               WHERE Z.BUKRS = TMP_DATA.BUKRS COLLATE Korean_Wansung_CI_AS
                                 AND Z.WERKS = TMP_DATA.WERKS COLLATE Korean_Wansung_CI_AS
                                 AND Z.MBLNR = TMP_DATA.MBLNR COLLATE Korean_Wansung_CI_AS
                                 AND Z.MJAHR = TMP_DATA.MJAHR COLLATE Korean_Wansung_CI_AS
                                 AND Z.ZEILE = TMP_DATA.ZEILE)   
    
            INSERT INTO dbo.WMS_PM270
                  (BUKRS,INSEQ,MBLNR,MJAHR,ZEILE,
                   MATCOD,
                   EBELN,EBELP,RSNUM,RSPOS,
                   INNO,INDAY,
                   PLTDIV,EPTDIV,
                   WERKS,LGORT,LIFNR,
                   NAME1,
                   BWART,MTART,SOBSL,LAND1,
                   INOKQTY,ENDQTY,
                   PRSDIV,IEDIV,
                   RTDIV,DELDIV,
                   USNAM,SYSDAY)
            SELECT BUKRS,INSEQ,MBLNR,MJAHR,ZEILE,
                   MATCOD,
                   EBELN,EBELP,RSNUM,RSPOS,
                   INNO,INDAY,
                   PLTDIV,EPTDIV,
                   WERKS,LGORT,LIFNR,
                   NAME1,
                   BWART,MTART,SOBSL,LAND1,
                   INOKQTY,ENDQTY,
                   PRSDIV,IEDIV,
                   RTDIV,DELDIV,
                   USNAM,SYSDAY
             FROM #T_PM270 TMP_DATA 
             WHERE  NOT EXISTS(SELECT Z.MBLNR   --lvwenting add2014-02-17
                                FROM WMS_PM270 Z
                               WHERE Z.BUKRS = TMP_DATA.BUKRS 
                                 AND Z.WERKS = TMP_DATA.WERKS 
                                 AND Z.MBLNR = TMP_DATA.MBLNR 
                                 AND Z.MJAHR = TMP_DATA.MJAHR 
                                 AND Z.ZEILE = TMP_DATA.ZEILE)  
    
             DELETE FROM #T_PM270 
    
             INSERT INTO #T_PM270 
                  (BUKRS,INSEQ,MBLNR,MJAHR,ZEILE,
                   MATCOD,
                   EBELN,EBELP,RSNUM,RSPOS,
                   INNO,INDAY,
                   PLTDIV,EPTDIV,
                   WERKS,LGORT,LIFNR,
                   NAME1,
                   BWART,MTART,SOBSL,LAND1,
                   INOKQTY,ENDQTY,
                   PRSDIV,IEDIV,
                   RTDIV,DELDIV,
                   USNAM,SYSDAY)
            SELECT TMP_DATA.BUKRS,
                   SUBSTRING(CONVERT(NVARCHAR(11),10000000000+((ROW_NUMBER() OVER(ORDER BY TMP_DATA.BUKRS,TMP_DATA.WERKS,TMP_DATA.MBLNR,TMP_DATA.MJAHR,TMP_DATA.ZEILE,TMP_DATA.RSNUM,TMP_DATA.RSPOS,TMP_DATA.MATCOD DESC))+ISNULL((SELECT MAX(CONVERT(DECIMAL(10,0),ISNULL(Z.INSEQ,0))) FROM  [DY_WMS].[dbo].[CW_PM270] Z ),0))),2,10) AS INSEQ,
                   TMP_DATA.MBLNR,TMP_DATA.MJAHR,TMP_DATA.ZEILE,
                   TMP_DATA.MATCOD,
                   TMP_DATA.EBELN,TMP_DATA.EBELP,TMP_DATA.RSNUM,TMP_DATA.RSPOS,
                   TMP_DATA.INNO,TMP_DATA.INDAY,
                   TMP_DATA.PLTDIV,TMP_DATA.EPTDIV,
                   TMP_DATA.WERKS,TMP_DATA.LGPRO,TMP_DATA.LIFNR,
                   TMP_DATA.NAME1,
                   TMP_DATA.BWART,TMP_DATA.MTART,TMP_DATA.SOBSL,TMP_DATA.LAND1,
                   TMP_DATA.INOKQTY, TMP_DATA.ENDQTY,
                   TMP_DATA.PRSDIV, TMP_DATA.IEDIV,
                   TMP_DATA.RTDIV, TMP_DATA.DELDIV,
                   TMP_DATA.USNAM,TMP_DATA.SYSDAY
            FROM (
            SELECT PP30T.BUKRS, '' AS MBLNR,'' AS MJAHR,0 AS ZEILE,
                   PP30T.MATNR AS MATCOD,
                   '' AS EBELN,0 AS EBELP,
                   PP30T.RSNUM,PP30T.RSPOS,
                   '' AS INNO,GETDATE() AS INDAY,
                   SUBSTRING(PP30T.WERKS,1,2) AS  PLTDIV,SUBSTRING(PP30T.WERKS,2,1) AS EPTDIV,
                   PP30T.WERKS,PP30T.UMLGO AS LGPRO,PP30T.LGPRO AS LIFNR,
                   CONVERT(NVARCHAR(35),(SELECT TOP 1 description FROM FC01M WHERE BUKRS = PP30T.BUKRS and Codeid = 'AD03' AND LTRIM(RTRIM(CodeValue)) = PP30T.LGPRO)) AS NAME1,
                   PP30T.BWART,AD10M.MTART,AD10M.SOBSL,'CN' AS LAND1,
                   CONVERT(DECIMAL(13,3),ISNULL(PP30T.BDMNG,0)) AS INOKQTY,0.000 AS ENDQTY,
                   '10' AS PRSDIV,      
                   '1' AS IEDIV,
                   '2' AS RTDIV,
                   'N' AS DELDIV,
                   PP30T.AENAM AS USNAM,GETDATE() as SYSDAY
            FROM PP30T 
                 INNER JOIN AD10M
                 ON AD10M.BUKRS = PP30T.BUKRS
                    AND AD10M.WERKS = PP30T.WERKS
                    AND AD10M.MATNR = PP30T.MATNR
                    AND AD10M.LOVEM = 'N'   
            WHERE PP30T.BUKRS = @S_BUKRS
              AND PP30T.WERKS = @S_WERKS
              AND PP30T.BWART = 'Z14'
              AND PP30T.ENMNG = 0
              AND PP30T.KZEAR = 'N'
              AND PP30T.LOVEM = 'N'  
              AND PP30T.LAEDA BETWEEN DATEADD(DAY,@V_PRE_DAY,GETDATE()) AND GETDATE()
              AND PP30T.UMLGO IN (SELECT Z.CodeValue 
                                    FROM FC01M Z
                                   WHERE Z.BUKRS = @S_BUKRS
                                     and Z.Codeid = 'AD03' 
                                     and Z.uddec1 = CONVERT(DECIMAL(13,3),@S_WERKS))  
              AND NOT EXISTS(SELECT Z.RSNUM
                               FROM  [DY_WMS].[dbo].[CW_PM270] Z
                              WHERE Z.BUKRS = PP30T.BUKRS COLLATE Korean_Wansung_CI_AS
                                AND Z.WERKS = PP30T.WERKS COLLATE Korean_Wansung_CI_AS
                                AND Z.RSNUM = PP30T.RSNUM COLLATE Korean_Wansung_CI_AS
                                AND Z.RSPOS = PP30T.RSPOS )                     
            UNION ALL
              (--유상사급 반입(Y03)인 경우
              SELECT SD20T.BUKRS, '' AS MBLNR,'' AS MJAHR,0 AS ZEILE,
                     SD21T.MATNR AS MATCOD,
                     '' AS EBELN,0 AS  EBELP,
                     SD21T.VBELN AS RSNUM,SD21T.VBELP AS RSPOS,         
                     '' AS INNO,GETDATE() AS INDAY,
                     SUBSTRING(SD20T.WERKS,1,2) AS  PLTDIV,SUBSTRING(SD20T.WERKS,2,1) AS EPTDIV,
                     SD20T.WERKS,SD21T.LGPRO,SD20T.KUNNR AS LIFNR,
                     CONVERT(NVARCHAR(35),FC20M.CSTNM) AS NAME1,
                     'Y03' AS BWART,AD10M.MTART,AD10M.SOBSL,FC20M.CNTRY AS LAND1,
                     CONVERT(DECIMAL(13,3),ISNULL(SD21T.MENGE,0)) AS INOKQTY,0.000 AS ENDQTY,
                     '10' AS PRSDIV,      
                     FC20M.DFDIV AS IEDIV,
                     '2' AS RTDIV,
                     'N' AS DELDIV,
                     SD21T.AENAM AS USNAM,GETDATE() as SYSDAY
               FROM  SD20T
                     INNER JOIN SD21T 
                     ON SD20T.BUKRS = SD21T.BUKRS 
                        AND SD20T.VBELN = SD21T.VBELN   
                        AND SD21T.LOVEM = 'N'        
                     INNER JOIN AD10M
                     ON AD10M.BUKRS = SD21T.BUKRS
                        AND AD10M.WERKS = SD21T.WERKS
                        AND AD10M.MATNR = SD21T.MATNR 
                        AND AD10M.LOVEM = 'N'     
                     INNER JOIN FC20M
                     ON FC20M.BUKRS = SD20T.BUKRS
                        AND FC20M.CSTNO = SD20T.KUNNR
                        AND FC20M.LOVEM = 'N' 
                where SD20T.BUKRS = @S_BUKRS
                  AND SD20T.WERKS = @S_WERKS    
                  AND SD21T.LAEDA BETWEEN DATEADD(DAY,@V_PRE_DAY,GETDATE()) AND GETDATE()
                  AND SD20T.AUART = 'ZUR'
                  AND SD21T.CENGE = 0
                  AND SD21T.LOVEM = 'N'
                  AND SD21T.LGPRO IN (SELECT Z.CodeValue 
                                        FROM FC01M Z
                                       WHERE Z.BUKRS = @S_BUKRS
                                         and Z.Codeid = 'AD03' 
                                         and Z.uddec1 = CONVERT(DECIMAL(13,3),@S_WERKS))
                  AND NOT EXISTS(SELECT Z.RSNUM
                                   FROM  [DY_WMS].[dbo].[CW_PM270] Z
                                  WHERE Z.BUKRS = SD21T.BUKRS COLLATE Korean_Wansung_CI_AS
                                    AND Z.WERKS = SD21T.WERKS COLLATE Korean_Wansung_CI_AS
                                    AND Z.RSNUM = SD21T.VBELN COLLATE Korean_Wansung_CI_AS
                                    AND Z.RSPOS = SD21T.VBELP )   
                ) 
              --2013.07.17 GSH 추가 : 311,325자재예약관리에서 처리로 인한 입고대기I/F대상 추가           
              UNION ALL
              (
                SELECT PP30T.BUKRS, '' AS MBLNR,'' AS MJAHR,0 AS ZEILE,
                       PP30T.MATNR AS MATCOD,
                       '' AS EBELN,0 AS EBELP,
                       PP30T.RSNUM,PP30T.RSPOS,
                       '' AS INNO,GETDATE() AS INDAY,
                       SUBSTRING(PP30T.WERKS,1,2) AS  PLTDIV,SUBSTRING(PP30T.WERKS,2,1) AS EPTDIV,
                       PP30T.WERKS,PP30T.UMLGO AS LGPRO,PP30T.LGPRO AS LIFNR,
                       CONVERT(NVARCHAR(35),(SELECT TOP 1 description FROM FC01M WHERE BUKRS = PP30T.BUKRS and Codeid = 'AD03' AND LTRIM(RTRIM(CodeValue)) = PP30T.LGPRO)) AS NAME1,
                       PP30T.BWART,AD10M.MTART,AD10M.SOBSL,'CN' AS LAND1,
                       CONVERT(DECIMAL(13,3),ISNULL(PP30T.BDMNG,0)) AS INOKQTY,0.000 AS ENDQTY,
                       '10' AS PRSDIV,      
                       '1' AS IEDIV,
                       '2' AS RTDIV,
                       'N' AS DELDIV,
                       PP30T.AENAM AS USNAM,GETDATE() as SYSDAY
                FROM PP30T 
                     INNER JOIN AD10M
                     ON AD10M.BUKRS = PP30T.BUKRS
                        AND AD10M.WERKS = PP30T.WERKS
                        AND AD10M.MATNR = PP30T.MATNR
                        AND AD10M.LOVEM = 'N'   
                WHERE PP30T.BUKRS = @S_BUKRS
                  AND PP30T.WERKS = @S_WERKS
                  AND PP30T.BWART IN ('311','325')
                  AND PP30T.ENMNG = 0
                  AND PP30T.KZEAR = 'N'
                  AND PP30T.LOVEM = 'N'  
                  AND PP30T.LAEDA BETWEEN DATEADD(DAY,@V_PRE_DAY,GETDATE()) AND GETDATE()
                  AND PP30T.LGPRO NOT IN (SELECT Z.CodeValue 
                                            FROM FC01M Z
                                           WHERE Z.BUKRS = @S_BUKRS
                                             and Z.Codeid = 'AD03' 
                                             and Z.uddec1 = CONVERT(DECIMAL(13,3),@S_WERKS)) 
                  AND PP30T.UMLGO IN (SELECT Z.CodeValue 
                                        FROM FC01M Z
                                       WHERE Z.BUKRS = @S_BUKRS
                                         and Z.Codeid = 'AD03' 
                                         and Z.uddec1 = CONVERT(DECIMAL(13,3),@S_WERKS))  
                  AND NOT EXISTS(SELECT Z.RSNUM
                                   FROM  [DY_WMS].[dbo].[CW_PM270] Z
                                  WHERE Z.BUKRS = PP30T.BUKRS COLLATE Korean_Wansung_CI_AS
                                    AND Z.WERKS = PP30T.WERKS COLLATE Korean_Wansung_CI_AS
                                    AND Z.RSNUM = PP30T.RSNUM COLLATE Korean_Wansung_CI_AS
                                    AND Z.RSPOS = PP30T.RSPOS )    
              ) 
            ) TMP_DATA  
    
    
            INSERT INTO  [DY_WMS].[dbo].[CW_PM270]
                  (BUKRS,INSEQ,MBLNR,MJAHR,ZEILE,
                   MATCOD,
                   EBELN,EBELP,RSNUM,RSPOS,
                   INNO,INDAY,
                   PLTDIV,EPTDIV,
                   WERKS,LGORT,LIFNR,
                   NAME1,
                   BWART,MTART,SOBSL,LAND1,
                   INOKQTY,ENDQTY,
                   PRSDIV,IEDIV,
                   RTDIV,DELDIV,
                   USNAM,SYSDAY)
            SELECT BUKRS,INSEQ,MBLNR,MJAHR,ZEILE,
                   MATCOD,
                   EBELN,EBELP,RSNUM,RSPOS,
                   INNO,INDAY,
                   PLTDIV,EPTDIV,
                   WERKS,LGORT,LIFNR,
                   NAME1,
                   BWART,MTART,SOBSL,LAND1,
                   INOKQTY,ENDQTY,
                   PRSDIV,IEDIV,
                   RTDIV,DELDIV,
                   USNAM,SYSDAY
             FROM #T_PM270      
    
            INSERT INTO dbo.WMS_PM270
                  (BUKRS,INSEQ,MBLNR,MJAHR,ZEILE,
                   MATCOD,
                   EBELN,EBELP,RSNUM,RSPOS,
                   INNO,INDAY,
                   PLTDIV,EPTDIV,
                   WERKS,LGORT,LIFNR,
                   NAME1,
                   BWART,MTART,SOBSL,LAND1,
                   INOKQTY,ENDQTY,
                   PRSDIV,IEDIV,
                   RTDIV,DELDIV,
                   USNAM,SYSDAY)
            SELECT BUKRS,INSEQ,MBLNR,MJAHR,ZEILE,
                   MATCOD,
                   EBELN,EBELP,RSNUM,RSPOS,
                   INNO,INDAY,
                   PLTDIV,EPTDIV,
                   WERKS,LGORT,LIFNR,
                   NAME1,
                   BWART,MTART,SOBSL,LAND1,
                   INOKQTY,ENDQTY,
                   PRSDIV,IEDIV,
                   RTDIV,DELDIV,
                   USNAM,SYSDAY
             FROM #T_PM270 
    
            DROP TABLE #T_PM270
                    
            
            IF @@ERROR <> 0
            BEGIN
                ROLLBACK TRANSACTION
                SET @V_CONTENTS = 'ERROR'
                EXEC [dbo].[USP_SEND_EMAIL] @V_EMAIL_WMS,@V_EMAIL_IT,@V_SUBJECT,@V_CONTENTS,'',0,'',0           
                RETURN
            END
    
            COMMIT  TRANSACTION     
                     
        END TRY
        BEGIN CATCH
        
             ROLLBACK TRANSACTION
             
             --오류시 오류문 리턴
             SELECT 'E', ERROR_MESSAGE();   
             
             --오류 표시
             PRINT 'Outer TRY error mumber: ' + CONVERT(varchar,ERROR_NUMBER())+ 
                   ' on line: ' + CONVERT(varchar, ERROR_LINE());                
             
             SET @V_CONTENTS = ERROR_MESSAGE()      
             EXEC [dbo].[USP_SEND_EMAIL] @V_EMAIL_WMS,@V_EMAIL_IT,@V_SUBJECT,@V_CONTENTS,'',0,'',0   
                   
        END CATCH;
        
        PRINT '저장프로시저 USP_WMS_PM270가 실행되었습니다.' 
        
        SET XACT_ABORT OFF
    END
    
    

    相关文章

      网友评论

          本文标题:SQL SERVER作业Interface

          本文链接:https://www.haomeiwen.com/subject/dqzqsxtx.html