Wednesday, 18 September 2013

un expected error getting while executing pivot stored procedure

un expected error getting while executing pivot stored procedure

This is my stored procedure:
ALTER procedure [dbo].[performancepivot]
@startdate nvarchar(100), @enddate nvarchar(100)
as
begin
declare @date1 nvarchar(100) = convert(varchar, @startdate+'
00:00:00.000', 120)
declare @date2 nvarchar(100) = convert(varchar, @enddate+'
23:59:59.000', 120)
DECLARE @cols AS NVARCHAR(MAX),@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(Vtype)
from VType_tbl
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') ,1,1,'')
set @query = 'SELECT LocName, ' + @cols + '(
select l.LocName,v.Vtype,[dbo].[testfunctionstacknew](
CONVERT(decimal(10,1), AVG( CONVERT(NUMERIC(18,2),
DATEDIFF(SS,t.Paydate,t.DelDate) ) ))) as Average
from (select l.LocName,Vtype from Transaction_tbl t join
VType_tbl v on t.vtid = v.vtid join dbo.Location_tbl l on
t.locid=l.Locid where dtime between '''+ @date1 +''' and '''+
@date2 +'''
and Status = 5) d pivot ( count(Vtype) for Vtype in (' + @cols +
')) p '
print @query
exec sp_executesql @query;
end
while executing this, I am getting Error like this:
SELECT LocName, [Emaar Staff],[Lost Ticket],[Normal],[VIP],[VVIP](
select l.LocName,v.Vtype,[dbo].[testfunctionstacknew](
CONVERT(decimal(10,1), AVG( CONVERT(NUMERIC(18,2),
DATEDIFF(SS,t.Paydate,t.DelDate) ) ))) as Average
from (select l.LocName,Vtype from Transaction_tbl t join
VType_tbl v on t.vtid = v.vtid join dbo.Location_tbl l on
t.locid=l.Locid where dtime between '2013-01-01 00:00:00.000' and
'2013-08-01 23:59:59.000'
and Status = 5) d pivot ( count(Vtype) for Vtype in ([Emaar
Staff],[Lost Ticket],[Normal],[VIP],[VVIP])) p
Msg 102, Level 15, State 1, Line 8 Incorrect syntax near 'p'.
(1 row(s) affected) what is wrong with my stored procedure

No comments:

Post a Comment