SQL查询到JOIN字段重命名Access VBA中的字段

时间:2016-07-21 18:49:05

标签: sql vba ms-access access-vba

我有一个Access VBA程序,它连接两个表并将它们存储在另一个表中。我的问题是,新表没有加入commonField的SQL查询,并且新表包含commonField字段,新表的commonField名为tableName1_commonField而另一个名为tableName2_commonField {1}}。

因此,如果查询如下:

SELECT IIPM.*, UNIX.* INTO [UNIX_lob] FROM IIPM INNER JOIN UNIX ON IIPM.[appcode_0] = 
UNIX.[appcode_0];

然后appcode_0IIPM之间共有的UNIX字段在新表IIPM_appcode_0中变为两个名为UNIX_appcode_oUNIX_lob的字段当我希望它保持单个字段appcode_0时。

执行SQL Query后,我已经跟踪了这个:

' Set the name of the new table where the joins will be stored. '
newTableName = "[" + tableName2 + "_lob]"

' Join tableName1 and tableName2 on commonField, and store the joined data in newTableName '
sqlJoinQuery = "SELECT " + tableName1 + ".*, " + tableName2 + ".*" & _
             " INTO " + newTableName & _
             " FROM " + tableName1 & _
             " INNER JOIN " + tableName2 & _
             " ON " + tableName1 + "." + commonField + " = " + tableName2 + "." + commonField + ";"
Debug.Print sqlJoinQuery
CurrentDb.Execute sqlJoinQuery

1 个答案:

答案 0 :(得分:1)

在写完上面的评论之后,我看到了你的另一个问题,所以我想我会扩展我上面所说的内容:

CREATE TABLE JoinTable (table1_key long null, table1_somecolumn text(50) null, table2_key long null, table2_somecolumn text(50) null);

然后完成了这个,

INSERT INTO JoinTable (Table1_key, table1_somecolumn, table2_key, table2_somecolumn)
SELECT t1.key, t1.somecolumn, t2.key, t2.somecolumn
FROM Table1 t1 INNER JOIN Table2 t2 ON t1.key = t2.key

这应该做你以后的事情(虽然毫无疑问你的查询看起来会比这些更麻烦!)

希望这有帮助