create table t(userid int,configname varchar(10),configvalue varchar(10))
insert into t select 1,'a','4'
insert into t select 1,'b','ff'
insert into t select 1,'c','100'
insert into t select 1,'d','rr'
insert into t select 2,'a','6'
insert into t select 2,'b','iiiiii'
insert into t select 2,'c','1000'
insert into t select 2,'d','hhh'
insert into t select 3,'a','9'
insert into t select 3,'b','fdsa'
insert into t select 3,'c','5555'
insert into t select 3,'d','dfdad'declare @sql varchar(8000)
set @sql='select userid as id'
select @sql=@sql+',['+configname+']=max(case configname when '''+configname+''' then configvalue end)'
from t group by configname order by configname
set @sql=@sql+' from t group by userid'
exec(@sql)如何把结果保存到表中(行列数不确定),最好是整体复制
insert into t select 1,'a','4'
insert into t select 1,'b','ff'
insert into t select 1,'c','100'
insert into t select 1,'d','rr'
insert into t select 2,'a','6'
insert into t select 2,'b','iiiiii'
insert into t select 2,'c','1000'
insert into t select 2,'d','hhh'
insert into t select 3,'a','9'
insert into t select 3,'b','fdsa'
insert into t select 3,'c','5555'
insert into t select 3,'d','dfdad'declare @sql varchar(8000)
set @sql='select userid as id'
select @sql=@sql+',['+configname+']=max(case configname when '''+configname+''' then configvalue end)'
from t group by configname order by configname
set @sql=@sql+' from t group by userid'
exec(@sql)如何把结果保存到表中(行列数不确定),最好是整体复制
-->
into tablename from t group by configname order by configname
insert into t select 1,'a','4'
insert into t select 1,'b','ff'
insert into t select 1,'c','100'
insert into t select 1,'d','rr'
insert into t select 2,'a','6'
insert into t select 2,'b','iiiiii'
insert into t select 2,'c','1000'
insert into t select 2,'d','hhh'
insert into t select 3,'a','9'
insert into t select 3,'b','fdsa'
insert into t select 3,'c','5555'
insert into t select 3,'d','dfdad'declare @sql varchar(8000)
set @sql='select userid as id'
select @sql=@sql+',['+configname+']=max(case configname when '''+configname+''' then configvalue end)'
from t group by configname order by configname
set @sql=@sql+' into T2 from t group by userid'
exec(@sql)select * from T2--result
id a b c d
----------- ---------- ---------- ---------- ----------
1 4 ff 100 rr
2 6 iiiiii 1000 hhh
3 9 fdsa 5555 dfdad(3 row(s) affected)
insert into t select 1,'a','4'
insert into t select 1,'b','ff'
insert into t select 1,'c','100'
insert into t select 1,'d','rr'
insert into t select 2,'a','6'
insert into t select 2,'b','iiiiii'
insert into t select 2,'c','1000'
insert into t select 2,'d','hhh'
insert into t select 3,'a','9'
insert into t select 3,'b','fdsa'
insert into t select 3,'c','5555'
insert into t select 3,'d','dfdad'declare @sql varchar(8000)
set @sql='select userid as id'
select @sql=@sql+',['+configname+']=max(case configname when '''+configname+''' then configvalue end)'
from t group by configname order by configname
set @sql=@sql+' INTO NewShippers from t group by userid'exec(@sql)select * from NewShippers