有两张表
peoplelist表
id name pid
1 张三 0
2 李四 0
3 王五 0
4 孙大 0
5 赵小 0
......
peoplename表
id name
1 赵小
2 孙大
3 李四
4 张三
5 王五
......
现在想peoplelist的pid对应peoplename的id,
也就是变为
peoplelist表
id name pid
1 张三 4
2 李四 3
3 王五 5
4 孙大 2
5 赵小 1
......数据有近10万条,数据库是sql server,
不知道有什么快捷高效的方法,谢谢
peoplelist表
id name pid
1 张三 0
2 李四 0
3 王五 0
4 孙大 0
5 赵小 0
......
peoplename表
id name
1 赵小
2 孙大
3 李四
4 张三
5 王五
......
现在想peoplelist的pid对应peoplename的id,
也就是变为
peoplelist表
id name pid
1 张三 4
2 李四 3
3 王五 5
4 孙大 2
5 赵小 1
......数据有近10万条,数据库是sql server,
不知道有什么快捷高效的方法,谢谢
select a.id , a.name , b.id as pid into #dk
from peoplelist as a , peoplename as b where a.name = b.name
truncatetable peoplelist
go
insert into peoplelist (id , name , pid) select id , name , pid from #dkdrop table #dk
go
insert into @t select 1,'张三',0
union all select 2,'李四',0
union all select 3,'王五',0
union all select 4,'孙大',0
union all select 5,'赵小',0declare @a table(id int,name varchar(10))
insert into @a select 1,'赵小'
union all select 2,'孙大'
union all select 3,'李四'
union all select 4,'张三'
union all select 5,'王五'update a set a.pid=b.id from @t a,@a b where a.name=b.nameselect * from @t--这样?
所以最好update,
AquaAndVita()兄的写法可能会产生重复数据。
其实,就算有,稍微调整下,就可以啦。外键的话,最好先drop掉,搞完再加回来。
楼主,就用俺的吧,可快了,我就老这么搞滴,哈哈,不蒙你。
create unique index u_name on peoplename
go
--2更新
update peplelist set pid=a.id from peoplename a where peoplelist.name=a.name
--1先对peoplename表建立索引
create unique index u_name on peoplename
go
--2更新
update peplelist set pid=peoplename.id from peoplename where peoplelist.name=peoplename .name