从TableB匹配更新TableA

时间:2017-10-23 19:31:27

标签: sql ms-access access-vba

我有两个表,两个表上的第一列匹配,我需要UPDATE TableA包含来自TableB的所有数据,仅TableA.Column1TableB.Column1匹配。我很难绕过如何让它工作...... TableA有大约80多列,TableB大约有100多列。我在Microsoft Access中尝试此操作。

表A

+---------+---------+---------+---------+---------+---------+
| Column1 | Column2 | Column3 | Column4 | ....... | ColumnX |
+---------+---------+---------+---------+---------+---------+
|    1    |  DataA  |         |  DataA  | ....... |         |
+---------+---------+---------+---------+---------+---------+
|    2    |  DataA  |         |  DataA  | ....... |         |
+---------+---------+---------+---------+---------+---------+
|    3    |  DataA  |         |  DataA  | ....... |         |
+---------+---------+---------+---------+---------+---------+
|    4    |  DataA  |         |  DataA  | ....... |         |
+---------+---------+---------+---------+---------+---------+
| ....... | ....... | ....... | ....... | ....... | ....... |
+---------+---------+---------+---------+---------+---------+
|    X    |  DataA  |         |  DataA  | ....... |         |
+---------+---------+---------+---------+---------+---------+

表B

+---------+---------+---------+---------+---------+---------+
| Column1 | Column2 | Column3 | Column4 | ....... | ColumnX |
+---------+---------+---------+---------+---------+---------+
|    1    |         |  DataB  |         | ....... |  DataB  |
+---------+---------+---------+---------+---------+---------+
|    2    |         |  DataB  |         | ....... |  DataB  |
+---------+---------+---------+---------+---------+---------+
|    3    |         |  DataB  |         | ....... |  DataB  |
+---------+---------+---------+---------+---------+---------+
|    4    |         |  DataB  |         | ....... |  DataB  |
+---------+---------+---------+---------+---------+---------+
| ....... | ....... | ....... | ....... | ....... | ....... |
+---------+---------+---------+---------+---------+---------+
|    X    |         |  DataB  |         | ....... |  DataB  |
+---------+---------+---------+---------+---------+---------+

TableA的最终结果

+---------+---------+---------+---------+---------+---------+
| Column1 | Column2 | Column3 | Column4 | ....... | ColumnX |
+---------+---------+---------+---------+---------+---------+
|    1    |  DataA  |  DataB  |  DataA  | ....... |  DataB  |
+---------+---------+---------+---------+---------+---------+
|    2    |  DataA  |  DataB  |  DataA  | ....... |  DataB  |
+---------+---------+---------+---------+---------+---------+
|    3    |  DataA  |  DataB  |  DataA  | ....... |  DataB  |
+---------+---------+---------+---------+---------+---------+
|    4    |  DataA  |  DataB  |  DataA  | ....... |  DataB  |
+---------+---------+---------+---------+---------+---------+
| ....... | ....... | ....... | ....... | ....... | ....... |
+---------+---------+---------+---------+---------+---------+
|    X    |  DataA  |  DataB  |  DataA  | ....... |  DataB  |
+---------+---------+---------+---------+---------+---------+

到目前为止我尝试过的事情......

UNION我很快放弃了...似乎只是给了我一个查询,我需要实际UPDATE TableA与TableB中的所有数据

还有......

UPDATE TableA ([Column1],[Column2],[Column3],[Column4], ....... [ColumnX])
SELECT [Column1],[Column2],[Column3],[Column4], ....... [ColumnX]
  FROM TableB
 WHERE (TableA.Column1 = TableB.Column2)

编辑1:

试过这个......

  

运行时错误' 3144':UPDATE语句中的语法错误

SQL = "   UPDATE tbleVendorData AS A " & _
      "     JOIN tbleOLD AS B " & _
      "       ON A.[Column1] = B.[Column1] " & _
      "      SET A.[Column14] = CONCAT(A.[Column14], B.[Column5])"

编辑2:

enter image description here

3 个答案:

答案 0 :(得分:0)

以下查询可能有效:

UPDATE TableA 
INNER JOIN TableB 
 ON(TableA.Column1 = TableB.Column2)
SET 
    TableA.Column3 = TableB.column3,
    ...
    TableA.Columnx = TableB.columnx
Where TableA.Column1 = TableB.column1;

希望它有所帮助!

答案 1 :(得分:0)

玩游戏:

UPDATE TableA AS A JOIN TableB AS B ON A.Column1 = B.Column1 SET A.Column1 = CONCAT(A.Column1,B.Column1) ...

或者在MSSQL中

UPDATE A SET A.[Column14] = A.[Column14] + B.[Column5] FROM tbleVendorData AS A JOIN tbleOLD AS B ON A.[Column1] = B.[Column1]

如果某个表中的任何给定字段的值在第二个字段中的相应字段中不为空,那么您可以尝试使用CONCAT(例如,A.Column1,B.Column1)。它并不聪明,但比使用IF更容易。

答案 2 :(得分:0)

不幸的是,这些建议都不起作用......但是经过深入挖掘后,下面的工作对我有用了!

_none