使用pivot命令时出现动态SQL错误

时间:2016-09-16 02:11:13

标签: mysql sql sql-server tsql

IF OBJECT_ID('tempdb..#temp1') IS NOT NULL DROP TABLE #temp1
GO

CREATE TABLE #TEMP1 
(
  COUNTER nvarchar(200),SEQUENCE INT   
)
INSERT INTO #TEMP1 VALUES ('Counter1','1')
INSERT INTO #TEMP1 VALUES ('Counter2','2')
INSERT INTO #TEMP1 VALUES ('Counter3','3')
INSERT INTO #TEMP1 VALUES ('Counter4','4')
INSERT INTO #TEMP1 VALUES ('Counter5','5')
INSERT INTO #TEMP1 VALUES ('Counter6','6')
INSERT INTO #TEMP1 VALUES ('Counter7','7')
INSERT INTO #TEMP1 VALUES ('Counter8','8')
INSERT INTO #TEMP1 VALUES ('Counter9','9')
INSERT INTO #TEMP1 VALUES ('Counter10','10')

--select * from #TEMP1


DECLARE @query AS NVARCHAR(MAX)=''
DECLARE @Counter nvarchar(1000)
DECLARE @Cols As NVARCHAR(MAX)=''

declare  MyCursor cursor
for SELECT  DISTINCT A.SEQ
    FROM tblSEQUENCE A 
    open MyCursor
fetch next from MyCursor into @Counter
while @@FETCH_STATUS=0
BEGIN

    Set @Cols = @Cols + 'Counter'+@Counter+''   
    fetch next from MyCursor into @Counter
END
CLOSE MyCursor;  
DEALLOCATE MyCursor
set @Cols= stuff(@Cols,len(@Cols),1,'')
print @Cols

set @query = 'SELECT ' + @cols + ' FROM (select B.METER_VALUE As VALUE,A.SEQ AS SEQUENCE,T.COUNTER AS COUNTER
                    from tblCOUNTER B,tblSEQUENCE A INNER JOIN #TEMP1 T
                     ON A.SEQ=T.SEQUENCE)
                             x
            pivot 
            (
                max(VALUE)
                for COUNTER in (' + @cols + ')
            ) p '

--print @query
execute(@query)

我需要根据顺序将最大值转移到相应的计数器。

我的结果需要看起来像

Counter_1   Counter_2   Counter_3   Counter_4   Counter_5
29823       29823       29823       29823       29823

我怎样才能做到这一点?我经常得到错误太长。最大长度为128

1 个答案:

答案 0 :(得分:0)

IF OBJECT_ID('tempdb..#temp1') IS NOT NULL DROP TABLE #temp1
GO

CREATE TABLE #TEMP1 
(
COUNTER nvarchar(200),SEQUENCE INT   
)
INSERT INTO #TEMP1 VALUES ('Counter1','1')
INSERT INTO #TEMP1 VALUES ('Counter2','2')
INSERT INTO #TEMP1 VALUES ('Counter3','3')
INSERT INTO #TEMP1 VALUES ('Counter4','4')
INSERT INTO #TEMP1 VALUES ('Counter5','5')
INSERT INTO #TEMP1 VALUES ('Counter6','6')
INSERT INTO #TEMP1 VALUES ('Counter7','7')
INSERT INTO #TEMP1 VALUES ('Counter8','8')
INSERT INTO #TEMP1 VALUES ('Counter9','9')
INSERT INTO #TEMP1 VALUES ('Counter10','10')

--select * from #TEMP1


DECLARE @query AS NVARCHAR(MAX)=''
DECLARE @Counter nvarchar(1000)
DECLARE @Cols As NVARCHAR(MAX)=''
DECLARE @ColsName As NVARCHAR(MAX)=''

declare  MyCursor cursor
for SELECT  DISTINCT A.SEQ
FROM tblSEQUENCE A 
open MyCursor
fetch next from MyCursor into @Counter
while @@FETCH_STATUS=0
BEGIN

Set @Cols = @Cols + '[Counter'+ @Counter+'],' 
print @cols  

Set @ColsName = @ColsName + '[Counter' + @Counter + '] as Counter_'+ @Counter+',' 
print @ColsName  
fetch next from MyCursor into @Counter
END
CLOSE MyCursor;  
DEALLOCATE MyCursor
set @Cols= substring(@Cols,1,len(@Cols)-1)
print @Cols

set @ColsName= substring(@ColsName,1,len(@ColsName)-1)
print @ColsName


set @query = 'SELECT ' + @ColsName + ' FROM (select B.METER_VALUE As VALUE,A.SEQ AS SEQUENCE,T.COUNTER AS COUNTER
                from tblCOUNTER B inner join tblSEQUENCE A On B.seq=A.SEQ INNER JOIN #TEMP1 T
                 ON A.SEQ=T.SEQUENCE)
                         x
        pivot 
        (
            max(VALUE)
            for COUNTER in (' + @cols + ')
        ) p '

--print @query
execute(@query)