结合INSERT INTO语句

时间:2014-12-23 17:16:56

标签: sql vba ms-access insert access-vba

我正在尝试使用SQL到VBA,根据其他表中的数据收集表中的一些总数。我在下面粘贴了两个SQL语句。这些工作但他们将总计插入两个不同的记录。我想知道是否有办法将它们插入到同一记录中?任何帮助将不胜感激!

TotalVerified = "INSERT INTO Totals([TOTAL VERIFIED FORMULARIES]) " & _
    "SELECT COUNT([FORMULARY ID])" & _
    "FROM VerifiedFormularies"
TotalAvailableImport = "INSERT INTO Totals([TOTAL AVAILABLE FOR IMPORT]) " & _
"SELECT COUNT([FORMULARY ID]) " & _
"FROM ImportMetricsIDs"

编辑:

   totalVerified = "INSERT INTO Totals([TOTAL VERIFIED FORMULARIES], [TOTAL AVAILABLE FOR IMPORT], [TOTAL SHOULD BE IMPORTED]) " & _
"SELECT A.cnt, B.cnt, C.cnt " & _
"FROM (SELECT COUNT([FORMULARY ID]) as cnt " & _
    "FROM VerifiedFormularies " & _
") AS A " & _
", ( " & _
    "SELECT COUNT([FORMULARY ID]) as cnt " & _
    "FROM ImportMetricsIDs " & _
") as B " & _
", (SELECT COUNT([FORMULARY ID]) as cnt " & _
"WHERE [IMPORT STATUS]= ""Yes"" " & _
"FROM ShouldImportMetricsIDsTable " & _
") AS C "

2 个答案:

答案 0 :(得分:2)

这可能在ms-access中有效,也可能无效:

INSERT INTO Totals([TOTAL VERIFIED FORMULARIES], [TOTAL AVAILABLE FOR IMPORT], [TOTAL SHOULD BE IMPORTED])
SELECT A.cnt, B.cnt, C.cnt
FROM ( 
    SELECT COUNT([FORMULARY ID]) as cnt
    FROM VerifiedFormularies
) AS A
, (
    SELECT COUNT([FORMULARY ID]) as cnt
    FROM ImportMetricsIDs
) as B
, (
    SELECT COUNT([FORMULARY ID]) as cnt
    FROM ShouldImportMetricsIDsTable 
    WHERE [IMPORT STATUS]= 'Yes'
) AS C

答案 1 :(得分:1)

你尝试过这种方法吗?

insert into table
(field1, field2)
(subquery1), (subquery2)