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

求一更新的存储过程
A表有字段:
bm,num,storage,lx

B表有字段:
bm,num,storage

现在要根据A表中lx=1的记录查询出来,如果B表中bm和storage相等(即有同库同品种)的就更新B表对应记录的数量num

B没有找到同库同品种的就插入新的记录

------解决方案--------------------
update b set num=aa.num from (select bm,num,storage from a where lx=1) aa where aa.bm=b.bm and aa.storage=b.storage

insert b select a.bm,a.num,a.storage from a left join b on a.bm=b.bm and a.storage=b.stroage where lx=1 and b.bm is null