2017-01-11 113 views
1

我有這個疑問這是工作確定只更新表,如果字段爲空

UPDATE Master 
    SET Closed = Date(), 
     Status = 'Closed', 
     [Closure Reason] = 'Not on Extract' 
    WHERE NOT EXISTS (SELECT 1 
         FROM Extract 
         WHERE Master.Case = Extract.Case 
        ); 

我需要修改它添加另一個條件,即只做更新,如果主的價值。[關閉原因]爲NULL

但是,這給出了一個語法錯誤:

UPDATE Master SET Closed = Date(), Status = 'Closed', [Closure Reason] = 'Not on Extract' 
WHERE [Closure Reason] = "" AND 
WHERE NOT EXISTS (SELECT 1 
        FROM Extract 
        WHERE Master.Case = Extract.Case 
       ); 

我已經嘗試使用IS NULL而不是= 「」 無濟於事。

請問它有什麼問題?

+1

2項WHERE條款一AND條件運算符'IS NULL'應該爲你希望做什麼工作。您應該使用該結構顯示查詢。 –

回答

3
UPDATE Master SET Closed = Date(), Status = 'Closed', [Closure Reason] = 'Not on Extract' 
WHERE [Closure Reason] IS NULL 
AND NOT EXISTS (SELECT 1 
        FROM Extract 
        WHERE Master.Case = Extract.Case 
       ); 

您不能添加多個WHERE條款,您需要使用AND。如果您的要求是「只有在Master。[Closure Reason]爲NULL」的情況下才進行更新,則檢查NULL,而不是空字符串。

+1

謝謝@JaydipJ,錯過了額外的「AND」。 – HoneyBadger

1

您需要在not exists之前刪除第二個where

UPDATE Master SET Closed = Date(), Status = 'Closed', [Closure Reason] = 'Not on Extract' 
WHERE [Closure Reason] = "" AND 
NOT EXISTS (SELECT 1 
        FROM Extract 
        WHERE Master.Case = Extract.Case 
       ); 
1

您需要添加的,而不是具有類似於

WHERE NOT EXISTS (SELECT 1 
        FROM Extract 
        WHERE Master.Case = Extract.Case 
       ) 
AND [Closure Reason] IS NULL;