合并具有日期条件的sql

时间:2013-12-08 08:28:45

标签: sql stored-procedures

我已经创建了如下的合并存储过程,我想要实现的是以下场景:

  • 如果ProductList表(完整)中不存在ProductTRN,则合并新记录
  • 仅更新PU.CreateDate大于目标表的CreateDate的ProductList记录,即ProductList(未完成)

请告诉我如何实现上述第二种情况,谢谢

CREATE PROCEDURE [dbo].[usp_ProductList_Merge]
AS 
    BEGIN

    DECLARE @retValue INT
        BEGIN TRY
            IF OBJECT_ID('ProductList') IS NOT NULL 
                BEGIN
                    BEGIN TRANSACTION MergeConsumerTable
                    SET NOCOUNT ON;
                    MERGE dbo.ProductList AS target
                        USING 
                            ( SELECT    

                    PU.ProductTRN,
                    PU.ProductName,
                                PU.ProductDescription,
                                        PU.CreateDate

                              FROM      dbo.TmpProductList PU
                              WHERE     PU.ProductTRN = ProductTRN



                            ) AS source (  
                    ProductTRN,
                                        ProductName,
                                        ProductDescription
                    CreateDate)

                        ON ( (target.ProductTRN) = LOWER(source.ProductTRN)

                           )
                        WHEN MATCHED 
                            THEN 
        UPDATE                SET
                ProductTRN= source.ProductTRN 
                        WHEN NOT MATCHED 
                            THEN    
        INSERT  (


                  ProductTRN,
                  ProductName,
                  ProductDescription,
          CreateDate

                )             VALUES
                ( 
          source.ProductTRN,
                  source.ProductName,
                  source.ProductDescription, 
                  source.CreateDate,


                );
                    DELETE  PU
                    FROM    dbo.TmpProductList PU


                    COMMIT TRANSACTION MergeProductListTable
                    SET @retValue = 1
                    SELECT @retValue
                END
            ELSE 
                BEGIN
                SET @retValue = -1
                    SELECT  @retValue
                END
        END TRY
        BEGIN CATCH
            ROLLBACK TRANSACTION MergeProductListTable
            DECLARE @ErrorMsg VARCHAR(MAX);
            DECLARE @ErrorSeverity INT;
            DECLARE @ErrorState INT;
            SET @ErrorMsg = ERROR_MESSAGE();
            SET @ErrorSeverity = ERROR_SEVERITY();
            SET @ErrorState = ERROR_STATE();
        SET @retValue = 0
        SELECT @retValue
           -- SELECT  0 AS isSuccess
        END CATCH
    END

1 个答案:

答案 0 :(得分:0)

WITH Source AS (
   SELECT ProductTRN
         ,ProductName
         ,ProductDescription
         ,CreateDate
   FROM dbo.TmpProductList
)
MERGE ProductList AS Target
USING Source
      ON Target.ProductTRN = Source.ProductTRN
WHEN MATCHED
     AND Source.CreatedDate > Target.CreatedDate
THEN UPDATE SET
    ProductName = Source.ProductName
   ,ProductDescription = Source.ProductDescription
   ,CreateDate = Source.CreatedDate
WHEN NOT MATCHED BY TARGET
THEN INSERT (
    ProductTRN
   ,ProductName
   ,ProductDescription
   ,CreateDate
  )
  VALUES (
    Source.ProductTRN 
   ,Source.ProductName
   ,Source.ProductDescription
   ,Source.CreatedDate
  )