表A:
Rid productCode
1 2
2 3A中productCode的值是唯一的
表B
id ProductCode Rid
1 2 需更新的值
2 1 需更新的值
3 2 需更新的值需求:表A中ProductCode与表B中的ProductCode是一对多的关系。 请教如何写sql将表A中的Rid值根据productCode更新到表B中rid字段。
Rid productCode
1 2
2 3A中productCode的值是唯一的
表B
id ProductCode Rid
1 2 需更新的值
2 1 需更新的值
3 2 需更新的值需求:表A中ProductCode与表B中的ProductCode是一对多的关系。 请教如何写sql将表A中的Rid值根据productCode更新到表B中rid字段。
if object_id('tempdb.dbo.#A') is not null drop table #A
create table #A (Rid int,productCode int)
insert into #A
select 1,2 union all
select 2,3
--> 测试数据: #B
if object_id('tempdb.dbo.#B') is not null drop table #B
create table #B (id int,ProductCode int,Rid int)
insert into #B
select 1,2,null union all
select 2,1,null union all
select 3,2,nullupdate b set b.Rid=a.Rid from #A a join #B b on a.ProductCode=b.ProductCodeselect * from #B
/*
id ProductCode Rid
----------- ----------- -----------
1 2 1
2 1 NULL
3 2 1
*/