MS Access SQL:将SELECT INTO与UNION ALL查询一起使用

时间:2015-06-16 13:59:17

标签: sql vba ms-access-2010 union-all select-into

我正在尝试将已经正常工作的UNION查询转换为SELECT INTO,作为一组临时表的VBA过程的一部分。我这样做是因为Access存在性能问题。

当我尝试在插入INTO子句的情况下运行此查询时,出现此错误:

"An action query cannot be used as a row source."

我不完全确定为什么我不能执行它或如何在不创建更多查询的情况下绕过它。造成这种情况的原因是什么?如何解决?

SELECT DISTINCT A.*
FROM (SELECT DISTINCT
C.[CUSTOMER CODE], 
D.[FISCAL DAY], 
D.[TY INVOICE DATE] AS [Invoice Date], 
C.[CUSTOMER CODE]&" - "&D.[FISCAL DAY] AS CONCAT, 
IIF(D.[TY INVOICE DATE] Is Not Null, 'TY', Null) As TYLY

***INTO [FD CUST Missing- Temp]***

FROM [QR- CUST FD] AS C,
[QR- FD SALES] AS D) AS A

LEFT JOIN [QR- CUST FD] AS C
ON C.[FD CONCAT] = A.[CONCAT]
WHERE C.[FD CONCAT] Is Null

UNION ALL 

SELECT DISTINCT 
A.*

***INTO [FD Cust Missing- Temp]***

FROM (SELECT DISTINCT
C.[CUSTOMER CODE],
D.[FISCAL DAY],
D.[LY INVOICE DATE] AS [Invoice Date], 
C.[CUSTOMER CODE]&" - "&D.[FISCAL DAY] AS CONCAT, 
IIF(D.[LY INVOICE DATE] Is Not Null, 'LY', Null) As TYLY

FROM [QR- CUST FD] AS C,
[QR- FD SALES] AS D)  AS A

LEFT JOIN [QR- CUST FD] AS C
ON C.[FD CONCAT] = A.[CONCAT]
WHERE C.[FD CONCAT] IS NULL

;

1 个答案:

答案 0 :(得分:5)

您收到错误

  

操作查询不能用作行源。

因为4 values查询是操作查询(如INSERT,UPDATE或DELETE查询),它不返回结果集。因此Insert无关紧要。因此,例如,这不会起作用(导致上述错误消息):

5 columns

但是,您可以按如下方式重新组织该查询,这确实有效:

SELECT ... INTO NewTableName FROM ...