Microsoft Access和SQL - 当table.2上不存在记录时更新table.1值

时间:2015-01-27 23:26:37

标签: sql ms-access exists

我在使下面的代码工作时遇到了一些问题。如果Upload.Invoice表中没有匹配的发票,我想将未处理/未批准的表的状态字段更新为2,当它为7且发票字段不为空时。

我已尝试过以下SQL,但它没有更新符合条件的值。对于我的测试,应该有4个更新,0个正在完成。我通常使用设计视图,并且不确定代码中出错的地方。

UPDATE 
    [Unprocessed/Unapproved TC], 
    Upload 
SET 
    [Unprocessed/Unapproved TC].Status = 2, 
    [Unprocessed/Unapproved TC].[Resolution Date] = Date()
WHERE 
    (([Unprocessed/Unapproved TC].Status)=7) 
    AND 
    (([Unprocessed/Unapproved TC].Invoice) Is Not Null) 
    AND 
    (Not Exists 
        (
            SELECT 
                * 
            FROM 
                [Unprocessed/Unapproved TC]
            WHERE 
                [Unprocessed/Unapproved TC].Invoice = [Upload].Invoice
        )
    );

1 个答案:

答案 0 :(得分:0)

我认为你应该在not exists子句中查询upload表,并且只是在update子句中引用你要更新的表,所以请尝试这样做(但一定要在运行之前进行备份):

UPDATE 
    [Unprocessed/Unapproved TC]     
SET 
    [Unprocessed/Unapproved TC].Status = 2, 
    [Unprocessed/Unapproved TC].[Resolution Date] = Date()
WHERE 
    (([Unprocessed/Unapproved TC].Status)=7) 
    AND 
    (([Unprocessed/Unapproved TC].Invoice) Is Not Null) 
    AND 
    (Not Exists 
        (
            SELECT 
                * 
            FROM 
                [Upload]
            WHERE 
                [Unprocessed/Unapproved TC].Invoice = [Upload].Invoice
        )
    );