SQL SERVER作业Interface

用于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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,928评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,192评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,468评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,186评论 1 286
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,295评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,374评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,403评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,186评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,610评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,906评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,075评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,755评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,393评论 3 320
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,079评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,313评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,934评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,963评论 2 351

推荐阅读更多精彩内容