2013-02-25 50 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

相關問題