oracle条件判断之if else
1.条件判断语法
if 条件1 then 执行的逻辑1; elsif 条件2 then --条件1不满足的时候,会先判断是否满足条件2 执行的逻辑2; else ---条件1 和条件2 都不满足 执行的逻辑3; end if; ---结束 if else 条件判断
2.练习1
传入一个员工的编号
如果这个员工的工资小于1000,就给他加300奖金
如果这个员工的工资 1000 到 2000,就给他加200奖金
如果这个员工的工资 大于 2000,就给他加100奖金
select * from emp where empno=7788;
declare v_empno number := &input; v_sal number; begin select sal into v_sal from emp where empno = v_empno; if v_sal < 1000 then update emp set comm = nvl(comm, 0) + 300 where empno = v_empno; -- commit; elsif v_sal between 1000 and 2000 then --条件1不满足的时候,会先判断是否满足条件2 update emp set comm = nvl(comm, 0) + 200 where empno = v_empno; -- commit; else ---条件1 和条件2 都不满足 update emp set comm = nvl(comm, 0) + 100 where empno = v_empno; end if; ---结束 if else 条件判断 -- commit; end; select * from emp where empno=7788;
3.练习2
示例:用户输入 员工编号,判断 这个员工所在部门的人数,如果3个以内,则不变,如果5个以内 则 该部门所有员工降薪100,否则 所有员工降薪200
declare v_empno emp.empno%type := &input; v_count number; begin select count(1) into v_count from emp where deptno in (select deptno from emp where empno = v_empno); if v_count < 3 then null; elsif v_count >= 3 and v_count < 5 then update emp set sal = sal - 100 where deptno in (select deptno from emp where empno = v_empno); -- commit; else update emp set sal = sal - 200 where deptno in (select deptno from emp where empno = v_empno); end if; -- commit; end;
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论