Sql Server CTE语法错误...求助,好吗?

时间:2012-01-03 22:07:51

标签: sql-server sql-server-2008 common-table-expression

刚刚用sql server express弄湿了脚。我有一个相当复杂的子查询,我用cte简化它/它们。我收到错误“DatesNotNeeded附近的语法错误。”任何接受者?提前感谢...

WITH Symb AS
(
     SELECT Symbol
     FROM tblSymbolsMain
),

DatesNotNeeded AS
(
     SELECT Date
     FROM tblDailyPricingAndVol
     WHERE (tblDailyPricingAndVol.Symbol = Symb.Symbol)
),

WideDateRange AS
(
     SELECT TradingDate
     FROM tblTradingDays
     WHERE (TradingDate >= dbo.NextAvailableDataDownloadDateTime()) AND (TradingDate <= dbo.LatestAvailableDataDownloadDateTime())
),

DatesNeeded AS
(
     SELECT TradingDate
     FROM WideDateRange
     WHERE NOT EXISTS (DatesNotNeeded)
),

SELECT Symb.Symbol, DatesNeeded.TradingDate
FROM Symb CROSS JOIN DatesNeeded

1 个答案:

答案 0 :(得分:1)

您对DatesNeeded的查询存在缺陷。

 SELECT TradingDate
 FROM WideDateRange
 WHERE NOT EXISTS (DatesNotNeeded)

它应该更像是:

 SELECT TradingDate
 FROM WideDateRange wdr
 WHERE NOT EXISTS (SELECT 1 FROM DatesNotNeeded WHERE Date = wdr.TradingDate)