zoukankan      html  css  js  c++  java
  • PL/SQL与SQL(Oracle)Case语句

    (使用scott账户下的表)
    1.Oracle SQL语句的case语句写法:

    --sql中的case用于分支判断并返回某个值。
    select empno , ename, deptno ,  
    case deptno
      when 10 then '总经办'
      when 20 then '综管部'
      when 30 then '市场部'
      else '其他'
    end
    from emp;
    
    select empno , ename, deptno ,  
    case 
      when deptno=10 then '总经办'
      when deptno=20 then '综管部'
      when deptno=30 then '市场部'
      else '其他'
    end
    from emp;

    2.PL/SQL语句的case语句写法:

    plsql中语法1:

    case 字段|变量
           when 比对值 then 执行语句...;
           [when 比对值 then 执行语句...;]
           [else 执行语句... ;]

    plsql中语法2:

    case 
           when 表达式 then 执行语句...;
           [when 表达式 then 执行语句...;]
           [else 执行语句... ;]
       end case;
    

    PLSQL中的case可用于分支判断并<返回>,也可以用于分支判断<执行>
    用case判断,并把返回值赋给某变量

    
    declare
      v_dname varchar(20);
      v_deptno int:=10;
    begin
      v_dname := 
      case v_deptno
      when 10 then '总经办' --返回值不要分号
      when 20 then '综管部'
      when 30 then '市场部'
      else '其他'
      end;   --case结束时只用end
      dbms_output.put_line(v_dname);  
    end;
    
    declare
      v_dname varchar(20);
      v_deptno int:=10;
    begin
      v_dname := 
      case 
      when v_deptno=10 then '总经办'
      when v_deptno=20 then '综管部'
      when v_deptno=30 then '市场部'
      else '其他'
      end;
      dbms_output.put_line(v_dname);  
    end;
    

    用case判断,并在分支中给某变量赋值

    declare
      v_dname varchar(20);
      v_deptno int:=10;
    begin
      case v_deptno
      when 10 then v_dname:='总经办'; --分支判断中执行,分号结束
      when 20 then v_dname:='综管部';
      when 30 then v_dname:='市场部';
      else v_dname:='其他';
      end case; -- case结束时要end case;
      dbms_output.put_line(v_dname);  
    end;
    
    declare
      v_dname varchar(20);
      v_deptno int:=10;
    begin
      case 
      when v_deptno=10 then v_dname:='总经办';
      when v_deptno=20 then v_dname:='综管部';
      when v_deptno=30 then v_dname:='市场部';
      else v_dname:='其他';
      end case; 
      dbms_output.put_line(v_dname);  
    end;

    有待完善….

    http://huangxiutao.cn
  • 相关阅读:
    mysql int类型 int(11) 和int(2)区别
    mysql 用户 登陆 权限相关
    【转】ext4+delalloc造成单次写延迟增加的分析
    write 系统调用耗时长的原因
    【转】通过blktrace, debugfs分析磁盘IO
    win7 wifi热点
    【SystemTap】 Linux下安装使用SystemTap源码安装SystemTap
    pdflush进程介绍与优化【转】
    How to find per-process I/O statistics on Linux
    oom_killer
  • 原文地址:https://www.cnblogs.com/ss0xt/p/6667188.html
Copyright © 2011-2022 走看看