存储过程中使用“with rollup”时出现语法错误

时间:2012-07-30 20:01:53

标签: sql sql-server-2008

我是第一次尝试在sql server中编写存储过程,代码如下所示。在我在查询末尾添加“with rollup”时,它会显示错误“关键字附近的语法错误

DROP PROCEDURE FIRSTPROCEDURE
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE FIRSTPROCEDURE
@startdate nchar(8), @enddate nchar(8)
AS
BEGIN
SET NOCOUNT ON;
select Date, SUM(QT1), SUM(QTY2), SUM(qTY3) FROM dbo.TABLE1
where date between @startdate and @enddate 
group by Date
order by Date
WITH ROLLUP
END
GO

并尝试执行以下程序:

exec firstprocedure '20120501', '20120525'

1 个答案:

答案 0 :(得分:2)

With rollup需要在order by之前。它与group by

有关
select Date, SUM(QT1), SUM(QTY2), SUM(qTY3) FROM dbo.TABLE1 
where date between @startdate and @enddate  
group by Date WITH ROLLUP 
order by Date 

此外,如果使用日期数据类型存储和查询日期,则可以避免一大堆问题