记录更新数据用法

先查询:
 select * from emp e left join dept t on e.deptno=t.deptno) set sal =( select (case when e.job='MANAGER' and  t.loc='DALLAS' then sal *1.15
                    when e.job='CLERK' and t.loc='NEW YORK' then sal*0.95 else sal end) t
                 from emp e join dept t on e.deptno=t.deptno)
     
 select e.job,t.dname,e.sal, case when t.dname='SALES' and  e.sal<1500 then sal *1.15
                    when t.dname='RESEARCH' and e.job='CLERK' then sal*1.05 else sal end
                 from emp e join dept t on e.deptno=t.deptno
 再更新:update (select * from emp e left join dept t on e.deptno = t.deptno) T

   set T.sal = (case

                 when t.job = 'MANAGER' and t.loc = 'DALLAS' then

                  T.sal * 1.15

                 when t.job = 'CLERK' and t.loc = 'NEW YORK' then

                  T.sal * 0.95

                  when T.dname = 'SALES' and T.sal < 1500 then

                  T.sal * 1.15

                 when T.dname = 'RESEARCH' and T.job = 'CLERK' then

                  T.sal * 1.05

                 else

                  T.sal

               end);

更新方法二:

merge into emp m
using (select empno,
              (case
                when e.job = 'MANAGER' and t.loc = 'DALLAS' then
                 sal * 1.15
                when e.job = 'CLERK' and t.loc = 'NEW YORK' then
                 sal * 0.95
                when t.dname = 'SALES' and e.sal < 1500 then
                 sal * 1.15
                when t.dname = 'RESEARCH' and e.job = 'CLERK' then
                 sal * 1.05
                else
                 sal
              end) t
         from emp e
         join dept t
           on e.deptno = t.deptno) n
on (m.empno = n.empno)
when matched then
 update set m.sal = n.t;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值