zoukankan      html  css  js  c++  java
  • Oracle条件判断

    一. if/else

    语法:
    if 条件表达式 then
    语句块;
    if 条件表达式 then

    语句块
    end if;
    elsif 条件表达式 then
    语句块;
    ...
    else
    语句块;
    end if;
    举例:输入一个员工编号,给该员工涨奖金。策略是这样的:
    如果原来员工没有奖金,则把基本工资的百分之10作为奖金,如果原来员工的奖金低于1000,把奖金提升到
    1000,其他情况奖金提升百分之10。

    declare
    -- 声明奖金的变量
    v_comm emp.comm%type;
    begin
    -- 查询出员工的奖金
    select comm into v_comm from emp where empno=&no;
    -- 判断如果员工没有奖金,把基本工资的百分之10作为奖金
    if v_comm is null then
    update emp set comm=sal*0.1 where empno=&no;
    --如果原先奖金低于1000,提升到1000
    elsif v_comm<1000 then
    update emp set comm=1000 where empno=&no;
    -- 其他情况把奖金提升百分之10
    else
    update emp set comm=comm*1.1 where empno=&no;
    end if;

    二.case when

    case when 和java中switch语句比较像。
    java中switch:
    switch(变量名){
    case 变量值1 :

    语句块;
    break;
    case 变量值2 :
    语句块;
    break;
    .....
    default:
    语句块;
    }
    case when 语法:
    case 变量名
    when 变量值1 then
    语句块;
    when 变量值2 then
    语句块;
    ........
    else:
    语句块;
    end case;
    举例:根据用户输入的部门编号,输出不同的部门名称,要求使用case when实现,不查询dept表

    declare
    -- 声明部门编号的变量
    v_deptno emp.deptno%type:=&no;
    begin
    case v_deptno
    when 10 then
    dbms_output.put_line('技术部');
    when 20 then
    dbms_output.put_line('销售部');
    when 30 then
    dbms_output.put_line('公关部');
    when 40 then
    dbms_output.put_line('开发部');
    -- 其他情况
    else
    dbms_output.put_line('您要查找的部门不存在'); 
    end case;
    end;

    if else 都能把case when 替代。case when 也可以替代if else.
    语法:
    case
    when 条件表达式1 then
    语句块;
    when 条件表达式2 then
    语句块;
    ....
    else
    语句块;
    end case;
    举例:

    declare
    -- 声明奖金的变量
    v_comm emp.comm%type;
    begin
    -- 查询出员工的奖金
    select comm into v_comm from emp where empno=&no;
    -- 判断如果员工没有奖金,把基本工资的百分之10作为奖金
    case
    when v_comm is null then
    update emp set comm=sal*0.1 where empno=&no;
    --如果原先奖金低于1000,提升到1000
    when v_comm<1000 then
    update emp set comm=1000 where empno=&no;
    -- 其他情况把奖金提升百分之10
    else
    update emp set comm=comm*1.1 where empno=&no;
    end case;
    end;
  • 相关阅读:
    接口继承与归一化设计
    继承part1
    组合
    静态方法,小结
    类方法
    Java 集合各个接口特性
    PV操作示例详解
    什么是Java序列化,如何实现java序列化
    String简单知识点
    intValue()、parseInt()和valueOf
  • 原文地址:https://www.cnblogs.com/duguangming/p/10840844.html
Copyright © 2011-2022 走看看