存储过程中的动态枢轴

时间:2014-08-22 09:56:12

标签: sql sql-server pivot

exec proc_TestPivotCol 'SectionID','StudiedBy'
ALTER PROCEDURE [dbo].[proc_TestPivotCol]
@ColHeader nvarchar(10),
@RowHeader    nvarchar(max)
AS
BEGIN
DECLARE @q as NVARCHAR(MAX)
DECLARE @cols AS NVARCHAR(MAX)

select @cols ='
select STUFF((SELECT  '','' + QUOTENAME('+@ColHeader+') 
                    from Section
                    group by SectionID
                    order by SectionID
            FOR XML PATH(''''), TYPE
            ).value(''.'', ''NVARCHAR(MAX)'') 
        ,1,1,'''')'
      execute(@cols)



 set @q =' SELECT StudiedBy, '+@cols+'
            from  (select  '+ @RowHeader+','+ @ColHeader+' from ##tempF)
             X
            pivot 
            (
                count(CDFID)
                for ' + @ColHeader+' in ('+@cols+' )
            ) p '
execute(@q)
print(@q)                   
END

此过程的语法错误是什么 他们是一个错误:关键字'select'附近的语法不正确。 @cols返回[B],[C],[H],但我不明白@q

中有什么问题

0 个答案:

没有答案