如何使用SQL中的Left Join更新数据

如何使用SQL中的Left Join更新数据

--档案
truncate table Archive
insert into Archive (id,gg,PaperTypeID)values(1,null,1)
insert into Archive (id,gg,PaperTypeID)values(2,null,2)
insert into Archive (id,gg,PaperTypeID)values(3,null,1)
insert into Archive (id,gg,PaperTypeID)values(4,null,1)
insert into Archive (id,gg,PaperTypeID)values(5,null,2)
insert into Archive (id,gg,PaperTypeID)values(6,null,1)
insert into Archive (id,gg,PaperTypeID)values(7,null,1)
insert into Archive (id,gg,PaperTypeID)values(8,null,1)
insert into Archive (id,gg,PaperTypeID)values(9,null,2)
insert into Archive (id,gg,PaperTypeID)values(10,null,2)
select * from Archive

--规格尺寸/纸张类型
truncate table dbo.SPaperType
insert into dbo.SPaperType values(1,'A4');
insert into dbo.SPaperType values(2,'16k');
insert into dbo.SPaperType values(5,'32k');
select * from dbo.SPaperType

--需求  
update Archive set gg='A4' where PaperTypeID=1
update Archive set gg='16k' where PaperTypeID=2
update Archive set gg='A3' where PaperTypeID not in(1,2)

--方法1
update Archive set Archive.gg=(select p.Caption from SPaperType p where Archive.PaperTypeID=p.id)
--方法2
update Archive set Archive.gg=p.Caption from Archive a left join SPaperType p on a.PaperTypeID=p.id
--方法3
merge into Archive a
 using SPaperType p
 on (a.PaperTypeID=p.id)
 when matched then update set a.gg=p.caption
 when not matched then insert (gg) values('A3'); --will be insert new data,but is not update data


http://blog.csdn.net/cadenzasolo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值