使用动态列和列名称进行透视

时间:2013-09-12 22:36:27

标签: sql sql-server-2005 dynamic unpivot

我正在尝试使用以下格式的大量列来删除表:

PID UID col1 col2 col3...

下面的动态SQL几乎可以获得除列名称之外的所有内容。目标是在“ID”字段中填写发起unpivot值的列的名称。

-- Build list of cols we want to unpivot (skip PID & UID)
declare @cols nvarchar(max) 
select @cols = coalesce(@cols+N',', N'') + quotename(c.name) from syscolumns c
inner join sysobjects o on c.id = o.id and o.xtype = 'u'
where o.name = 'MyTable' and c.name not in ('PID', 'UID') order by c.colid

declare @query nvarchar(max)  

select @query = N'
select PID, [UID], ID, Val
from 
    (
    select PID, UID, ''ID'' as ID, ' + @cols + '
    from MyTable
    where UID <> 0
    ) as cp
    unpivot
    (
    Val for Vals in (' + @cols + ')
    ) as up
'
exec sp_executesql @query 

我想也许我可以用syscolumns&amp; MyTable,然后做第二次转移,但我无法弄明白。

最终我的查询应该返回

PID UID ID          Val

123 456 'col1 name' 'xyz'
123 456 'col2 name' 'def'
123 333 'col1 name' 'fdf'
...

因此,虽然我知道如何获取列的名称以便为unpivot生成动态SQL,但我不知道如何将列的名称加入到unpivot的输出中。

1 个答案:

答案 0 :(得分:3)

您可以从unpivot的val for col in部分引用列名。 Col获取列名称

Example Fiddle

-- Build list of cols we want to unpivot (skip PID & UID)
declare @cols nvarchar(max) 
select @cols = coalesce(@cols+N',', N'') + quotename(c.name) from syscolumns c
inner join sysobjects o on c.id = o.id and o.xtype = 'u'
where o.name = 'MyTable' and c.name not in ('PID', 'UID') order by c.colid

declare @query nvarchar(max)  

select @query = N'
select PID, [UID], Col as ID, Val
from 
    (
    select PID, UID, ' + @cols + '
    from MyTable
    where UID <> 0
    ) as cp
    unpivot
    (
    Val for Col in (' + @cols + ')
    ) as up
'
exec sp_executesql @query