22,210
社区成员
发帖
与我相关
我的任务
分享
select 项目='T1',类别=T1 from 表名
union all
select 项目='T2',类别=T3 from 表名
union all
select 项目='T3',类别=T3 from 表名
create table t
(col1 int,col2 int,col3 int)
insert t
select 1,2,3 union all
select 4,5,6
declare @sql varchar(8000),
@sql1 varchar(8000),
@sql2 varchar(8000),
@sql3 varchar(8000)
select @sql='',@sql1='select ''a'' ',@sql2='select ''b''',@sql3='select ''c'''
select @sql1=@sql1+','+cast(col1 as varchar),
@sql2=@sql2+','+cast(col2 as varchar),
@sql3=@sql3+','+cast(col3 as varchar)
from t
select @sql='create table #(col1 varchar(20),col2 int,col3 int) insert #
'+@sql1+' union all
'+@sql2+' union all
'+@sql3+'
select * from #
drop table #'
exec(@sql)
drop table t
col1 col2 col3
-------------------- ----------- -----------
a 1 4
b 2 5
c 3 6
select 项目='T1',类别=T1 from 表名
union all
select 项目='T2',类别=T3 from 表名
union all
select 项目='T3',类别=T3 from 表名
select 项目='T1', 類別=T1 from TT
union all
select 项目='T2', 類別=T2 from TT
union all
select 项目='T3', 類別=T3 from TT
select 'T1' as 項目,T1 as 類別 from tt union all
select 'T2' as 項目,T2 as 類別 from tt union all
select 'T3' as 項目,T3 as 類別 from tt
select 项目='T1',类别=T1 from 表名
union all
select 项目='T2',类别=T3 from 表名
union all
select 项目='T3',类别=T3 from 表名