I’m first time trying to write a stored procedure in sql server and the codes as below. Here when I add “with rollup” at the end of the query, it shows error “Incorrect syntax near the keyword 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
And tried to execute the procedure as below :
exec firstprocedure '20120501', '20120525'
With rollupneeds to come before theorder by. It’s related to thegroup byAlso, you’ll avoid a whole bunch of issues if you store and query your dates using the date datatype