给你SQL Server语句看看 ---------------------------------------------------------------- --建立测试表格 create table testTable (id int identity(1,1),name varchar(30))insert into testTable(name) select 'test' union all select 'wsss' union all select 'asdfa' union all select 'asdf'--输出结果declare @sql varchar(1000) set @sql = 'select distinct ''name'' as id' select @sql = @sql + ',''' + name + ''' as [' + cast(id as varchar(10)) + ']' from testTable exec(@sql + 'from testTable')/* id 1 2 3 4 ---- ---- ---- ----- ---- name test wsss asdfa asdf */--删除测试表格 drop table TestTable
----------------------------------------------------------------
--建立测试表格
create table testTable
(id int identity(1,1),name varchar(30))insert into testTable(name)
select 'test' union all
select 'wsss' union all
select 'asdfa' union all
select 'asdf'--输出结果declare @sql varchar(1000)
set @sql = 'select distinct ''name'' as id'
select @sql = @sql + ',''' + name + ''' as [' + cast(id as varchar(10)) + ']' from testTable
exec(@sql + 'from testTable')/*
id 1 2 3 4
---- ---- ---- ----- ----
name test wsss asdfa asdf
*/--删除测试表格
drop table TestTable