Sql 执行pivot存储过程时获取数据时发生意外错误

Sql 执行pivot存储过程时获取数据时发生意外错误,sql,sql-server,Sql,Sql Server,这是我的存储过程: 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(var

这是我的存储过程:

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,15级,状态1,第8行“p”附近语法不正确。(1 受影响的行(个)


我的存储过程有什么问题?

您的查询出现了结构和语法错误:

  • 我在试图跟上打开和关闭的
    的数量时迷路了。你应该开始看看这个
  • 不能在其他
    SELECT
    中使用嵌入的
    SELECT
    语句。嵌入式
    SELECT
    只能在
    FROM
    WHERE
    或具有
    语句的
    中使用。在您的查询中,子选择返回3个值,但您将它们引用为
    平均值
    (一个值)
  • 我强烈建议您直接编写存储过程,因为我可以看到您使用的查询构造只是嵌入变量,而不是实际构建查询。您可以将查询作为常规存储过程编写,并将其用作SP变量
  • 我在您的查询中添加了一个缺少的逗号,请检查

  • 你能格式化你的代码吗?