尝试创建存储过程时出现语法错误,尽管完全相同的代码作为查询运行完美.我自己也看不到错误,所以任何帮助都会受到赞赏.
错误是:
消息102,级别15,状态1,过程DataSelect,第12行'OrderedYTD'附近的语法不正确.
代码很简单:
CREATE PROCEDURE DataSelect
(
@TargetPdc int
)
AS
BEGIN
-- Refresh Data Here
EXEC DataUpdate
-- Select Data for Report
WITH OrderedYTD AS
(
SELECT custextract.*, histextract.*,
ROW_NUMBER () OVER (PARTITION BY custextract.custcustno ORDER BY histextract.salesytd desc) AS RowNumber
FROM custextract
INNER JOIN histextract
ON custextract.custcustno = histextract.histcustno
WHERE (custextract.ecall = 'Y')
)
SELECT OrderedYTD.*
FROM OrderedYTD
WHERE RowNumber <= 10 and pdc = @TargetPdc;
END
Run Code Online (Sandbox Code Playgroud)
我已经将所有从WITH语句开始的东西(减去WHERE子句中的变量)作为查询多次运行而没有任何问题.在存储过程中使用CTE是否存在语法差异?谢谢.