如果Field为NULL,则仅更新表

时间:2017-01-11 12:01:46

标签: sql ms-access

我有这个查询正常工作

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

我需要修改它以添加另一个标准,即只有Master的值才会更新。[Closure Reason]为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而不是=“”无效。

请问有什么问题?

3 个答案:

答案 0 :(得分: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 :(得分:1)

您需要在where之前删除第二个not exists

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
                 );

答案 2 :(得分:1)

您需要添加AND条件运算符,而不是像

那样有两个WHERE子句
WHERE NOT EXISTS (SELECT 1
                  FROM Extract
                  WHERE Master.Case = Extract.Case
                 )
AND [Closure Reason] IS NULL;