字段 id a b c d e f g h ......
1 2 4 5 3 2 6 3 b ......
2 3 4 5 1 6 a c d ......
字段很多,如何将ID为2的整行的值更新到字段1
求最简单的办法.(不要一一列出字段名)
4 个解决方案
#1
字段很多,如何将ID为2的整行的值更新到ID为1的行.
#2
create table t(id int,a int, b int,c int)
insert t select 1,2,3,4
insert t select 2,5,6,7
go
declare @sql varchar(8000)
set @sql='update a set a.'
select @sql=@sql+b.name+'=b.'+b.name+','
from sysobjects a,syscolumns b where b.id=a.id and a.id=object_id('t') and b.name<>'id'
set @sql=left(@sql,len(@sql)-1) +' from t a,t b where a.id=1 and b.id=2'
exec(@sql)
go
select * from t
/*结果
id a b c
----------- ----------- ----------- -----------
1 5 6 7
2 5 6 7
(所影响的行数为 2 行)*/
drop table t
insert t select 1,2,3,4
insert t select 2,5,6,7
go
declare @sql varchar(8000)
set @sql='update a set a.'
select @sql=@sql+b.name+'=b.'+b.name+','
from sysobjects a,syscolumns b where b.id=a.id and a.id=object_id('t') and b.name<>'id'
set @sql=left(@sql,len(@sql)-1) +' from t a,t b where a.id=1 and b.id=2'
exec(@sql)
go
select * from t
/*结果
id a b c
----------- ----------- ----------- -----------
1 5 6 7
2 5 6 7
(所影响的行数为 2 行)*/
drop table t
#3
up
#4
谢谢:zhaoanle(zhao)
即时兑现,给分.
即时兑现,给分.
#1
字段很多,如何将ID为2的整行的值更新到ID为1的行.
#2
create table t(id int,a int, b int,c int)
insert t select 1,2,3,4
insert t select 2,5,6,7
go
declare @sql varchar(8000)
set @sql='update a set a.'
select @sql=@sql+b.name+'=b.'+b.name+','
from sysobjects a,syscolumns b where b.id=a.id and a.id=object_id('t') and b.name<>'id'
set @sql=left(@sql,len(@sql)-1) +' from t a,t b where a.id=1 and b.id=2'
exec(@sql)
go
select * from t
/*结果
id a b c
----------- ----------- ----------- -----------
1 5 6 7
2 5 6 7
(所影响的行数为 2 行)*/
drop table t
insert t select 1,2,3,4
insert t select 2,5,6,7
go
declare @sql varchar(8000)
set @sql='update a set a.'
select @sql=@sql+b.name+'=b.'+b.name+','
from sysobjects a,syscolumns b where b.id=a.id and a.id=object_id('t') and b.name<>'id'
set @sql=left(@sql,len(@sql)-1) +' from t a,t b where a.id=1 and b.id=2'
exec(@sql)
go
select * from t
/*结果
id a b c
----------- ----------- ----------- -----------
1 5 6 7
2 5 6 7
(所影响的行数为 2 行)*/
drop table t
#3
up
#4
谢谢:zhaoanle(zhao)
即时兑现,给分.
即时兑现,给分.