Oracle之学习if条件选择语句

Oracle之学习if条件选择语句

订阅号文章:Oracle之学习if条件选择语句

  1. 摘要
    这是夜说的第三篇学习文章,学习plsql的if条件选择语句。

  2. 条件选择语句

    if语句
    利用if语句实现控制时,根据选择条件个数的不同,可以分为以下三种情况:
    if then
    if then else
    if then elsif

    注意:if语句必须以end if作为结束

1. if then语句的语法为:
if condition then
   statements
end if;
其中,condition为返回逻辑值的表达式、变量、常量等,如果返回值为真,则执行
statements,否则不执行任何操作,结束该语句的执行。

例1:查询50号部门的平均工资,如果平均工资低于5000元,则每个员工工资增加1000元。
SQL> set serveroutput on
SQL> declare
  2     v_avgsal employees.salary%type;
  3  begin
  4     select avg(salary) into v_avgsal from employees where department_id = 50;
  5  if v_avgsal < 5000 then
  6     update employees set salary = salary + 1000 where department_id = 50;
  7  end if;
  8  end;
  9  /
  
PL/SQL procedure successfully completed.


2.if then else语句的语法为:
if condition then
     statements
else
     else_statements
end if;
如果condition条件为真,则执行then短语后面的statements语句。如果条件为假,
则执行else短语后面的else_statements语句。

例2:查询110号员工的工资,如果工资大于8000元,则工资增加500元。如果工资低于
8000元,则工资增加1000元。
SQL> set serveroutput on
SQL> declare
  2     v_salary employees.salary%type;
  3  begin
  4     select salary into v_salary from employees where employee_id = 110;
  5  if v_salary > 8000 then
  6     update employees set salary = salary + 500 where employee_id = 110;
  7  else
  8     update employees set salary = salary + 1000 where employee_id = 110;
  9  end if;
 10  end;
 11  /
 
PL/SQL procedure successfully completed.


if语句可以相互嵌套。在if语句的执行语句statements和else_statements中都可以
嵌套if语句。
例3:if语句的嵌套示例
SQL> set serveroutput on
SQL> declare
  2     procedure p (sales number,quota number,emp_id number)
  3  is
  4     bonus number := 0;
  5  begin
  6     if sales > (quota + 200) then
  7     bonus := (sales - quota)/4;
  8     else
  9             if sales > quota then
 10             bonus := 50;
 11             else
 12             bonus := 0;
 13             end if;
 14  end if;
 15  dbms_output.put_line('bonus = '||bonus);
 16  update employees set salary = salary + bonus where employee_id = emp_id;
 17  end p;
 18  begin
 19     p(10100,10000,120);
 20     p(10500,10000,121);
 21     p(9500,10000,122);
 22  end;
 23  /
bonus = 50
bonus = 125
bonus = 0

PL/SQL procedure successfully completed.


if then elsif语句的语法为:
if condition1 then
  statements1
 elsif condition2 then
  statements2
[elsif condition3 then
  statements3  
] ...
[else 
  else_statements
]
end if;
如果程序有多个条件选择,不同选择有不同的逻辑处理,可以使用if then elsif语句。
执行第一个condition为真,其后的语句不再执行。如果所有的condition都不为真,
执行else短语的else_statements语句。如果没有else短语,则结束if语句的执行。

例4:输入一个员工号,如果该员工在50号部门,则工资增加1000元。在60号部门,
    则工资增加1500元。在90号部门,则工资增加2000元。在其他部门,则工资增加
    2500元。
SQL> declare
  2     v_deptno employees.department_id%type;
  3     v_increment number(4);
  4     v_empno employees.employee_id%type;
  5  begin
  6     v_empno := &x;
  7     select department_id into v_deptno from employees where employee_id = v_empno;
  8     if v_deptno = 50 then
  9             v_increment := 1000;
 10     elsif v_deptno = 60 then
 11             v_increment := 1500;
 12     elsif v_deptno = 90 then
 13             v_increment := 2000;
 14     else
 15             v_increment := 2500;
 16     end if;
 17     update employees set salary = salary + v_increment where employee_id = v_empno;
 18  end;
 19  /
Enter value for x: 110
old   6:        v_empno := &x;
new   6:        v_empno := 110;

PL/SQL procedure successfully completed.


条件为null时的处理:
由于plsql的逻辑值有true、false和null三种。所以在进行条件判断的时候还要考虑
null的情况。为了避免条件为null时产生歧义,应该在进行条件是否为null的检查,
对条件为null、true和false三种情况都进行处理。

SQL> declare
  2     v_number1 number;
  3     v_number2 number := 150;
  4     v_result char(20);
  5  begin
  6     if v_number1 is null or v_number2 is null then
  7             v_result := 'UNKNOW';
  8     elsif v_number1 > v_number2 then --v_number1 is null
  9             v_result := 'YES';
 10     else
 11             v_result := 'NO';
 12     end if;
 13  dbms_output.put_line(v_result);
 14  end;
 15  /
UNKNOW

PL/SQL procedure successfully completed.
  • 4
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值