0

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

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'
4

1 回答 1

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 

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

于 2012-07-30T20:04:55.470 回答