使用SQL使用Table2的主键值更新Table1的主键值

时间:2018-03-23 12:25:59

标签: sql vba ms-access access-vba

在两个表中,UserID字段是主键,标准是FistName字段值或MobileNumber字段值必须匹配。表格如下:

Table1
+--------+-----------+---------------+
+ UserID + FirstName + MobileNumber  +
+--------+-----------+---------------+
+  100   + James     + 07725252511   +
+--------+-----------+---------------+
+  200   + Joy       + 07721323111   +
+--------+-----------+---------------+
+  300   + Pike      + 07820010012   +
+--------+-----------+---------------+

Table2
+--------+-----------+---------------+
+ UserID + FirstName + MobileNumber  +
+--------+-----------+---------------+
+  100   + James     + 07725252511   +
+--------+-----------+---------------+
+  210   + Joy       + 07721323111   +
+--------+-----------+---------------+
+  31a   + Pike      + 07820010012   +
+--------+-----------+---------------+

运行SQL之后,我希望Table1看起来像:

+--------+-----------+---------------+
+ UserID + FirstName + MobileNumber  +
+--------+-----------+---------------+
+  100   + James     + 07725252511   +
+--------+-----------+---------------+
+  210   + Joy       + 07721323111   +
+--------+-----------+---------------+
+  31a   + Pike      + 07820010012   +
+--------+-----------+---------------+

我有这个代码运行但由于密钥违规错误而未更新Table1的UserID字段值。这里有什么帮助吗?

SQL = "UPDATE Table1 " & _
"INNER JOIN Table2 " & _
"ON(Table1.FirstName = Table2.FirstName OR Table1.MobileNumber = 
 Table2.MobileNumber) " & _
"SET Table1.UserID = Table2.UserID, " & _
"Table1.Age = Table2.Age; "

DoCmd.RunSQL SQL

0 个答案:

没有答案