从Ms Access中的另一个表的列更新表列

时间:2014-06-11 11:04:29

标签: sql sql-update ms-access-2010

在Access 2010中,我有两个相同的表,比如说A和B.

每个表的结构是

key | text_column1 | text_column2

我必须更新表A的行,如果对于A中的给定行,两个文本列为空,则必须给出表B中的值。

around,我试过了

UPDATE  A as a
SET a.text_column1 = (SELECT b.text_column1 FROM B AS b WHERE b.key = a.key),
a.text_column2 = (SELECT b.text_column2 FROM B AS b WHERE b.key = a.key)
WHERE a.text_column1 IS NULL and a.text_column2

不用说以前的查询不起作用。我得到了error 3073 (2010)

2 个答案:

答案 0 :(得分:2)

您可以使用Access的UPDATE .. JOIN语法:

UPDATE  A as a
        INNER JOIN B as b
            ON a.Key = b.Key
SET     a.text_column1 = b.text_column1,
        a.text_column2 = b.text_column2
WHERE   a.text_column1 IS NULL
AND     a.text_column2 IS NULL;

另一种选择是使用Dlookup

UPDATE  A
SET     text_column1 = Dlookup("text_column1", "B", "Key = "& A.Key),
        text_column2 = Dlookup("text_column2", "B", "Key = "& A.Key)
WHERE   a.text_column1 IS NULL
AND     a.text_column2 IS NULL;

答案 1 :(得分:0)

试试这个

UPDATE  A
SET A.text_column1 = (SELECT b.text_column1 FROM B AS b WHERE b.key = A.key),
A.text_column2 = (SELECT b.text_column2 FROM B AS b WHERE b.key = A.key)
WHERE A.text_column1 IS NULL and A.text_column2 IS NULL;