SQL SERVER 问题,高手来,在线求解
一张表中有300多个列,我想把这300个列插入到新表中的一个列中,请问各位老师这样的问题改怎么解决?
[解决办法]
declare @s nvarchar(max)select @s=isnull(@s+' union all select ',' select ')+quotename(Name)+' from 表名' from syscolumns where ID=object_ID('表名')exec('insert NewTable (COl)'+@s)
[解决办法]
举个10列数据抽取5列做行列转换的例子,不知是不是楼主所要的,
create table t5(id int,a int,b int,c int,d int,e int,f int,g int,h int,i int,j int)insert into t5 select 101,1,2,3,4,5,6,7,8,9,10 union allselect 102,11,22,33,44,55,66,77,88,99,1010select id,b,d,f,h,j from t5id b d f h j----------- ----------- ----------- ----------- ----------- -----------101 2 4 6 8 10102 22 44 66 88 1010select id,cfrom (select id,b,d,f,h,j from t5) tunpivot (c for cc in (b,d,f,h,j)) utid c----------- -----------101 2101 4101 6101 8101 10102 22102 44102 66102 88102 1010