我正在尝试将T-SQL查询转换为MS Access SQL,并得到了我一直在寻找的语法错误。我的MS Access SQL查询如下所示:
INSERT INTO IndvRFM_PreSort (CustNum, IndvID, IndvRScore, IndRecency, IndvFreq, IndvMonVal )
SELECT
IndvMast.CustNum, IndvMast.IndvID, IndvMast.IndvRScore,
IndvMast.IndRecency, IndvMast.IndvFreq, IndvMast.IndvMonVal
FROM
IndvMast
INNER JOIN
OHdrMast ON IndvMast.IndvID = OHdrMast.IndvID
INNER JOIN
MyParameterSettings on 1=1].ProdClass
INNER JOIN
[SalesTerritoryFilter_Check all that apply] ON IndvMast.SalesTerr = [SalesTerritoryFilter_Check all that apply].SalesTerr
WHERE
(((OHdrMast.OrdDate) >= [MyParameterSettings].[RFM_StartDate]))
GROUP BY
IndvMast.CustNum, IndvMast.IndvID, IndvMast.IndvRScore,
IndvMast.IndRecency, IndvMast.IndvFreq, IndvMast.IndvMonVal,
[CustTypeFilter_Check all that apply].IncludeInRFM,
[ProductClassFilter_Check all that apply].IncludeInRFM,
[SourceCodeFilter_Check all that apply].IncludeInRFM,
IndvMast.FlgDontUse
我在http://rogersaccessblog.blogspot.com/2013/05/what-are-differences-between-access-sql.html和其他几个位置检查了MS Access SQL和T-SQL之间的区别,但是没有运气。
感谢所有帮助。
更新:我删除了许多行以查找语法错误,并且仅在运行时仍会遇到相同的错误(使用T-SQL可以正常运行):
SELECT
IndvMast.CustNum, IndvMast.IndvID, IndvMast.IndvRScore,
IndvMast.IndRecency, IndvMast.IndvFreq, IndvMast.IndvMonVal
FROM
IndvMast
INNER JOIN
OHdrMast ON IndvMast.IndvID = OHdrMast.IndvID
INNER JOIN
[My Parameter Settings] ON 1 = 1
最佳答案
在任何符合SQL的数据库中,查询中有许多项目也应该失败:
GROUP BY
或FROM
子句中未引用的JOIN
中的表中的字段。 SELECT
查询中的字段数与INSERT INTO
子句中的字段数不匹配。 ON
表达式正确连接。 严格的MS Access SQL项目:
ON 1=1
子句中使用像WHERE
这样的表达式,并且对于交叉连接表,因为MyParameterSettings看起来像这样,请使用逗号分隔的表。 下面是对SQL语句的调整,以演示括号对,并为最佳做法使用表别名,特别是对于长表名。
INSERT INTO IndvRFM_PreSort (CustNum, IndvID, IndvRScore, IndRecency, IndvFreq, IndvMonVal)
SELECT
i.CustNum, i.IndvID, i.IndvRScore, i.IndRecency, i.IndvFreq, i.IndvMonVal
FROM
[MyParameterSettings] p, (IndvMast i
INNER JOIN
OHdrMast o ON i.IndvID = o.IndvID)
INNER JOIN
[SalesTerritoryFilter_Check all that apply] s ON i.SalesTerr = s.SalesTerr
WHERE
(o.OrdDate >= p.[RFM_StartDate])
GROUP BY
i.CustNum, i.IndvID, i.IndvRScore, i.IndRecency, i.IndvFreq, i.IndvMonVal
在较小的SQL子集中,最后一个表不需要
ON 1=1
条件,并且在SQL Server中也可能是多余的。如果您打算进行交叉连接,只需使用逗号分隔的表即可。在上面的示例中也是如此:SELECT
IndvMast.CustNum, IndvMast.IndvID, IndvMast.IndvRScore,
IndvMast.IndRecency, IndvMast.IndvFreq, IndvMast.IndvMonVal
FROM
[My Parameter Settings], IndvMast
INNER JOIN
OHdrMast ON IndvMast.IndvID = OHdrMast.IndvID
关于sql - Access SQL语法错误: missing operator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41595233/