动态生成更新查询

时间:2016-03-22 07:27:40

标签: sql-server sql-server-2008-r2 sql-update nullable isnull

我已经编写了以下存储过程,动态创建一个更新语句来更新产品表,其中包含从 Rule1 Rule2 获取的相应值strong>某些连接条件下的表格。

我遇到的问题是,当我将原始产品表值与来自联接的值进行比较时,更新查询是没有形成。

产品表中的原始值可能是 NULL ,也可能不是。我需要检查原始值是否不相同,然后使用值更新相应的列。

这是sp代码:

    CREATE PROCEDURE [dbo].[UpdateCTFamilyRules]
    AS     
    BEGIN       
        DECLARE @T_1 NVARCHAR(100) = NULL
        DECLARE @T_2 NVARCHAR(100) = NULL   
        DECLARE @Fl NVARCHAR(100) = NULL    

        DECLARE @T1 NVARCHAR(100) = NULL
        DECLARE @T2 NVARCHAR(100) = NULL    
        DECLARE @F NVARCHAR(100) = NULL 

    Select  @T_1 = p.T1, 
            @T_2 = p.T2, 
            @Fl = p.F
    from Product p  
    where p.Manufacturer_Name = 'Dell'; 


    Select  @T1 = r2.T1, 
            @T2 = r2.T2, 
            @F = r2.F
    from Product p
    join Rule1 r1 on p.Product_Code = r1.Product_Code
    join Rule2 r2 on r1.MajorCode = r2.Product_Family
    where p.Manufacturer_Name = 'Dell';         

    BEGIN       
        DECLARE @sqlCommand VARCHAR(8000)       
        DECLARE @Starter VARCHAR(100)

        SET @Starter = 'SET '
        SET @sqlCommand = 'UPDATE Product '

        BEGIN               
            IF ( @T1 IS NOT NULL) AND ( @T1 <> @T_1 )  BEGIN   SET @sqlCommand = @sqlCommand + @Starter + 'T1 = ''' + @T1 + ''''  SET @Starter = ', ' END
            IF ( @T2 IS NOT NULL) AND ( @T1 <> @T_2)  BEGIN   SET @sqlCommand = @sqlCommand + @Starter + 'T2 = ''' + @T2 + ''''  SET @Starter = ', ' END
            IF ( @F IS NOT NULL)  AND ( @F <> @Fl ) BEGIN   SET @sqlCommand = @sqlCommand + @Starter + 'F = ''' + @F + ''''  SET @Starter = ', ' END    
        END

        -- if any one column  get updated in DB then automatically starter sets to , then execute dynamic sqlCommand else ignore..

        IF (@Starter = ', ')
        BEGIN       
            SET @sqlCommand = @sqlCommand + @Starter + ' Last_Modify_Date = ''' + CONVERT(VARCHAR(22),SYSDATETIME(),121) + '''' 
            SET @sqlCommand = @sqlCommand + ' from Product p
                join Rule1 r1 on p.Product_Code = r1.Product_Code
                join Rule2 r2 on r1.MajorCode = r2.Product_Family 
                WHERE p.Manufacturer_Name = ''' + 'Dell' + ''''         
            EXEC (@sqlCommand)
       END      
    END 
END

请告诉我使用可空列进行比较的问题。如果是,那么我如何修改它。感谢。

1 个答案:

答案 0 :(得分:0)

为什么不在SQL Server中尝试MERGE UPDATE

MERGE Prodcut AS TARGET
USING (
        SELECT   R2.Manufacturer_Name, 
                 R2.T1, 
                 R2.T2, 
                 R2.F
        FROM     Product P
                 INNER JOIN Rule1 R1 ON P.Product_Code = R1.Product_Code
                 INNER JOIN Rule2 R2 ON R1.MajorCode = R2.Product_Family
       ) AS SOURCE ( Manufacturer_Name, T1, T2, F ) 
         ON (TARGET.Manufacturer_Name = SOURCE.Manufacturer_Name) -- Match on condition
WHEN MATCHED AND (TARGET.T1 <> SOURCE.T1 OR 
                  TARGET.T2 <> SOURCE.T2 OR 
                  TARGET.F <> SOURCE.F) -- Find non matched columns
THEN 
UPDATE SET   TARGET.T1 = SOURCE.T1, 
             TARGET.T2 = SOURCE.T2, 
             TARGET.F = SOURCE.F -- Update non matched columns

注意:请测试测试数据库中的代码&amp;根据需要进行修改。