sql 将多列数据转换成多行显示

表 t1 中有一行数据,有三个字段a,b,c
select * from t1
a b c
--------
A B C
怎么用一句sql让这三列数据变成三行显示,字段是d?
d
--
A
B
C
我想要一个通用的,有几列就有几行,比如那一行是5个字段:A B C D E,那这个就要显示为5行

select a as d from t1
union all
select b from t1
union all
select c from t1

通用只可用动态

declare @s nvarchar(4000)
select @s=isnull(@s+'union all select ','select ')+Name+' as D from 表名' from syscolumns where ID=object_id('表名')
exec(@s)
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答