日期:2014-05-17  浏览次数:20628 次

寻求判断编码是否相同然后更新价格的代码
数据库有两个表,表1和表2,结构如下

表1

bianma jiage
111AADDD 111
222ffg 21
3434ffggg 34


表2

Partsnum Partspri
111AADDD
122dfdf
2d232323


我想用表1的“bianma”与表2“partsnum”进行比对,如果有相同则用表1的“jiage”替换表2的“partspri”。寻求这样的SQL语句,多谢各位朋友,帮帮忙了。

------解决方案--------------------
update B set b.partspri=a.jiage from A JOIN B ON a.bianma=b.partsnum

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



update 表2
set partspri =表1.jiage
from 表1
where 表1.bianma = 表2.partsnum

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

create table #a (bianma varchar(20),jiage int )
create table #b(Partsnum varchar(20),Partspri int)
--drop table #b
--drop table #a
insert into #a select '111AADDD',111 UNION ALL
select '222ffg',21 UNION ALL
select '3434ffggg',34
insert into #b select '111AADDD',0 UNION ALL
select '1222dfdf',0 UNION ALL
select '2d232323',0
select * from #a
select * from #b
update b set b.Partspri=a.jiage from #a a,#b b where a.bianma=b.partsnum
--结果:
Partsnum   Partspri
111AADDD    111
1222dfdf    0
2d232323    0