通过SQL Server

时间:2016-04-05 12:40:40

标签: sql-server join stored-procedures exception-handling sql-update

我有三张表Table1Table2Table3。我在Table1 - >中有三列。 Tier1 Tier2Flagmay or may not be null。我根据常用列和updating这三列来加入这些表格。

我的代码如下:

ALTER PROCEDURE [dbo].[SpUpdateTable]
AS     
BEGIN 
     SET NOCOUNT ON;

     BEGIN TRANSACTION;
     BEGIN TRY    

        UPDATE Table1  
        SET Tier_1 = tbl3.T1, Modify_Date = SYSDATETIME()
        FROM Table1 tbl1
        JOIN Table2 tbl2 ON tbl1.CommonColumn1 = tbl2.CommonColumn1
        JOIN Table3 tbl3 ON tbl2.CommonColumn2 = tbl3.CommonColumn2
        WHERE tbl1.BrandName = 'Dell' AND 
        (tbl3.T1 IS NOT NULL AND tbl1.Tier_1 IS NULL OR tbl1.Tier_1 <>  tbl3.T1);

        UPDATE Table1  
        SET Tier_2 = tbl3.T2, Modify_Date = SYSDATETIME()
        FROM Table1 tbl1
        JOIN Table2 tbl2 ON tbl1.CommonColumn1 = tbl2.CommonColumn1
        JOIN Table3 tbl3 ON tbl2.CommonColumn2 = tbl3.CommonColumn2
        WHERE tbl1.BrandName = 'Dell' AND 
        (tbl3.T2 IS NOT NULL AND tbl1.Tier_2 IS NULL OR tbl1.Tier_2 <>  tbl3.T2);


        UPDATE Table1  
        SET Flag = tbl3.Flag, Modify_Date = SYSDATETIME()
        FROM Table1 tbl1
        JOIN Table2 tbl2 ON tbl1.CommonColumn1 = tbl2.CommonColumn1
        JOIN Table3 tbl3 ON tbl2.CommonColumn2 = tbl3.CommonColumn2
        WHERE tbl1.BrandName = 'Dell' AND 
        (tbl3.Flag IS NOT NULL AND tbl1.Flag IS NULL OR tbl1.Flag <> tbl3.Flag);

   END TRY 
   BEGIN CATCH
         IF @@TRANSCOUNT > 0
            ROLLBACK TRANSACTION;
            RETURN;        
   END CATCH

   IF @@TRANSCOUNT > 0
      COMMIT TRANSACTION;
END

我的代码是逐个更新列。我关心的是更新每一列hitting database,我们有什么方法可以使用单一更新查询来实现这一目标吗?

2 个答案:

答案 0 :(得分:0)

试试这个:

 BEGIN TRY    

    UPDATE Table1  
        SET Modify_Date = SYSDATETIME(),
            Tier_1 = CASE WHEN (tbl3.T1 IS NOT NULL AND tbl1.Tier_1 IS NULL OR tbl1.Tier_1 <>  tbl3.T1) THEN
                        tbl3.T1
                     ELSE 
                        Tier_1 
                     END,
            Tier_2 = CASE WHEN (tbl3.T2 IS NOT NULL AND tbl1.Tier_2 IS NULL OR tbl1.Tier_2 <>  tbl3.T2) THEN
                        tbl3.T2
                     ELSE 
                        Tier_2
                     END,
            Flag = CASE WHEN (tbl3.Flag IS NOT NULL AND tbl1.Flag IS NULL OR tbl1.Flag <> tbl3.Flag) THEN
                        tbl3.Flag
                     ELSE
                        Flag
                     END
    FROM Table1 tbl1
    JOIN Table2 tbl2 ON tbl1.CommonColumn1 = tbl2.CommonColumn1
    JOIN Table3 tbl3 ON tbl2.CommonColumn2 = tbl3.CommonColumn2
    WHERE tbl1.BrandName = 'Dell' 
    AND 
    (
        (tbl3.T1 IS NOT NULL AND tbl1.Tier_1 IS NULL OR tbl1.Tier_1 <>  tbl3.T1)
        OR 
        (tbl3.T2 IS NOT NULL AND tbl1.Tier_2 IS NULL OR tbl1.Tier_2 <>  tbl3.T2)
        OR
        (tbl3.Flag IS NOT NULL AND tbl1.Flag IS NULL OR tbl1.Flag <> tbl3.Flag)
    )
END TRY 
BEGIN CATCH
    --code to catch exception
END CATCH

答案 1 :(得分:0)

我会用这样的东西:

with x as (
    select tbl1.tier_1 tbl1_tier_1, tbl1.tier_2 tbl1_tier_2, tbl1.flag tbl1_flag, tbl3.t1, tbl3.t2, tbl3.flag
    from table1 tbl1
    join table2 tbl2 on tbl1.commoncolumn1 = tbl2.commoncolumn1
    join table3 tbl3 on tbl2.commoncolumn2 = tbl3.commoncolumn2
    where tbl1.brandname = 'Dell' 
)
update x 
set
    tbl1_tier_1 = isnull(tbl1_tier_1, t1),
    tbl1_tier_2 = isnull(tbl1_tier_2, t2),
    tbl1_tier_3 = isnull(tbl1_tier_3, t3),
    modify_date = sysdatetime()

只有当它为NULL时才会更新Table1中的值,否则它将保留现有值。