这是我的存储过程:
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
执行此操作时,我收到的错误如下:
SELECT LocName
,[Emaar Staff]
,[Lost Ticket]
,[Normal]
,[VIP]
,[VVIP]
, -- This Comma was missing added by MBD edit
(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'p'附近的语法不正确。 (1 行(受影响)
我的存储过程出了什么问题?
答案 0 :(得分:0)
您的查询遭遇结构和语法错误:
(
)
的数量。你应该开始考虑那个。SELECT
内使用嵌入式SELECT
语句。嵌入式SELECT
只能在FROM
,WHERE
或HAVING
声明中使用。在您的查询中,子选择返回3个值,但您将它们引用为Average
(一个值)