日期:2014-05-18  浏览次数:20468 次

根据一个人名单将人员表里的姓名更新
为了做演示数据,本来人员表里有好几千条数据,但姓名那一列都测试时录入的“ABC”,"A1"这些测试信息

我在网上找了一个真实的人名单,我已经把他导到一个临时表中了,我想把现有人员表的“姓名”字段更新为临时表里的“人名”

------解决方案--------------------
SQL code


declare mycursor cursor
for
select [name] from t1
open mycursor
select id=row_number()over(order by [name]),* into #tmp from t2--t2要修改的表 
declare @name money,@i int
set @i=1
fetch next from mycursor into @name
while @@fetch_status=0
begin
    update #tmp set [name]=@name where id=@i
    fetch next from mycursor into @name
    set @i=@i+1
    
end
delete from t2--删掉现有数据
alter table #tmp drop column id--去掉临时表中的rownumber列
insert into goal select * from #tmp--插入新数据
drop table #tmp
close mycursor
deallocate mycursor
---------------------------------------