日期:2014-05-17 浏览次数:20680 次
create table #ta1(id int ,tx varchar(20))
go
insert into #ta1
select 1,'小明' union all
select 2,'小红' union all
select 3,'小李'
go
create table #ta2(id int,Ftx varchar(20),Fid int)
go
insert into #ta2
select 1,'小明',0 union all
select 2,'小明',0 union all
select 3,'小李',0 union all
select 4,'小李',0 union all
select 5,'小军',0 union all
select 6,'小军',0
UPDATE a SET Fid=b.id
FROM #ta2 a
JOIN #ta1 b ON a.Ftx=b.tx
create table #ta1(id int ,tx varchar(20))
go
insert into #ta1
select 1,'小明' union all
select 2,'小红' union all
select 3,'小李'
go
create table #ta2(id int,Ftx varchar(20),Fid int)
go
insert into #ta2
select 1,'小明',0 union all
select 2,'小明',0 union all
select 3,'小李',0 union all
select 4,'小李',0 union all
select 5,'小军',0 union all
select 6,'小军',0
update #ta2
set Fid = t1.id
from #ta2 t2
inner join #ta1 t1
on t2.Ftx = t1.tx
select *
from #ta2
/*
id Ftx Fid
1 小明 1
2 小明 1
3 小李 3
4 小李 3
5 小军 0
6 小军 0
*/