2013-02-25 37 views

回答

0

使用CASE条件,如果新的值等于列的前值。

Update MyTable 
SET  ProductName = @ProductName, 
     ProductPrice = @ProductPrice, 
     Date_Updated = CASE WHEN (ProductName <> @ProductName) OR 
            (ProductPrice <> @ProductPrice) 
          THEN GETDATE() 
          ELSE Date_Updated 
         END 
WHERE ProductID = @ProductID 
+0

这看起来非常接近。我所面临的问题只有在更新预先存在的字段时才起作用。因此,让我们说ProductPrice是空的,我对ProductName进行了更改 - 它工作得很好。但是如果我第一次编辑并添加价格,它不会更新日期字段。如果我返回并更改该价格,它会更新日期字段。难道我做错了什么?非常感谢你的帮助。 – user1447679 2013-02-25 05:39:52

+0

如果你的意思是'ProductPrice'为null,试试这个附加条件'CASE WHEN(产品名称<> @ProductName)OR \t \t \t \t \t \t \t \t(ProductPrice <> @ProductPrice)OR \t \t \t \t \t \t \t \t(产品名称IS NULL)OR \t \t \t \t \t \t \t \t(ProductPrice IS NULL)' – 2013-02-25 05:43:02

+0

在我的快速示例中,我省略了几个字段:ProductDescription和其他几个字段......所以如果我这样做了,那么每次ProductPrice为null时都不会更新Date_Updated字段吗?即使其他领域不改变?无论如何,我会试试它! BRB!谢谢! – user1447679 2013-02-25 05:47:44

相关问题