尝试执行下面的SQL语句时遇到错误。不确定在WHERE周围是否还需要另一个保留字或括号,但我的想法已经用完了。
我收到了错误 - " Msg 156,Level 15,State 1,Line 20 关键字' WHERE'。"
附近的语法不正确任何人都可以帮我弄清楚如何纠正这个错误吗?
提前致谢!
SELECT dbo.PURCHASING_DETAIL.ID
,dbo.SCA_SS_INVOICELINESALL.*
,dbo.SCA_SS_INVOICESALL.*
,dbo.PURCHASING_DETAIL.QUOTEDPRICE
,dbo.PURCHASING_DETAIL.GLACCT
,dbo.PURCHASING_DETAIL.[PO DATE]
FROM dbo.PURCHASING_DETAIL
INNER JOIN (
(
dbo.SCA_SS_INVOICELINESALL INNER JOIN dbo.SCA_SS_INVOICESALL ON dbo.SCA_SS_INVOICELINESALL.INVOICESALLID = dbo.SCA_SS_INVOICESALL.INVOICESALLID
) INNER JOIN dbo.PURCHASING_DETAIL ON (dbo.SCA_SS_INVOICELINESALL.SUPPARTNUM = dbo.PURCHASING_DETAIL.SUPPARTNUM)
AND (dbo.SCA_SS_INVOICESALL.INVOICENUMBER = dbo.PURCHASING_DETAIL.INVOICENUM)
)
INNER JOIN dbo.SCA_SS_LOCATION ON (dbo.SCA_SS_INVOICESALL.SHIPTOADDRID = dbo.SCA_SS_LOCATION.LOCATIONID)
AND (dbo.PURCHASING_DETAIL.FACILITY = dbo.SCA_SS_LOCATION.FACILITYID)
WHERE (dbo.PURCHASING_DETAIL.[6DIGIT] = '50002')
AND DATEDIFF(month, [PO DATE], GETDATE()) <= 12
答案 0 :(得分:0)
您的查询中有一些不必要的JOIN(如重复的INNER JOIN dbo.PURCHASING_DETAIL
)和一些无用的括号。试试这个:
SELECT dbo.PURCHASING_DETAIL.ID,
dbo.SCA_SS_INVOICELINESALL.*,
dbo.SCA_SS_INVOICESALL.*,
dbo.PURCHASING_DETAIL.QUOTEDPRICE,
dbo.PURCHASING_DETAIL.GLACCT,
dbo.PURCHASING_DETAIL.[PO DATE]
FROM dbo.PURCHASING_DETAIL
JOIN dbo.SCA_SS_INVOICELINESALL
ON dbo.SCA_SS_INVOICELINESALL.SUPPARTNUM = dbo.PURCHASING_DETAIL.SUPPARTNUM
JOIN dbo.SCA_SS_INVOICESALL
ON dbo.SCA_SS_INVOICESALL.INVOICENUMBER = dbo.PURCHASING_DETAIL.INVOICENUM
JOIN dbo.SCA_SS_LOCATION
ON dbo.SCA_SS_INVOICESALL.SHIPTOADDRID = dbo.SCA_SS_LOCATION.LOCATIONID AND
dbo.PURCHASING_DETAIL.FACILITY = dbo.SCA_SS_LOCATION.FACILITYID
WHERE dbo.PURCHASING_DETAIL.[6DIGIT] = '50002'
AND DATEDIFF(month,[PO DATE], GETDATE()) <= 12
答案 1 :(得分:0)
您还有一些JOIN表达式引用不在连接中的表。这些应该在WHERE子句中。它会工作,但它真的很难读。
我也同意你应该删除所有的括号。
另外,正如我在评论中提到的那样,你有两次Purchasing_Detail。
试试这个:
SELECT dbo.PURCHASING_DETAIL.ID,
dbo.SCA_SS_INVOICELINESALL.*,
dbo.SCA_SS_INVOICESALL.*,
dbo.PURCHASING_DETAIL.QUOTEDPRICE,
dbo.PURCHASING_DETAIL.GLACCT,
dbo.PURCHASING_DETAIL.[PO DATE]
FROM dbo.SCA_SS_INVOICELINESALL
INNER JOIN dbo.SCA_SS_INVOICESALL
ON dbo.SCA_SS_INVOICELINESALL.INVOICESALLID = dbo.SCA_SS_INVOICESALL.INVOICESALLID
INNER JOIN dbo.PURCHASING_DETAIL
ON dbo.SCA_SS_INVOICELINESALL.SUPPARTNUM = dbo.PURCHASING_DETAIL.SUPPARTNUM
INNER JOIN dbo.SCA_SS_LOCATION
ON (dbo.SCA_SS_INVOICESALL.SHIPTOADDRID = dbo.SCA_SS_LOCATION.LOCATIONID)
WHERE (dbo.PURCHASING_DETAIL.[6DIGIT] = '50002')
AND (dbo.SCA_SS_INVOICESALL.INVOICENUMBER = dbo.PURCHASING_DETAIL.INVOICENUM)
AND (dbo.PURCHASING_DETAIL.FACILITY = dbo.SCA_SS_LOCATION.FACILITYID)
AND DATEDIFF(month,[PO DATE], GETDATE()) <= 12