我是第一次尝试在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'
答案 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
此外,如果使用日期数据类型存储和查询日期,则可以避免一大堆问题