• oracle存储过程或者Functioncase条件控制语句的用法


    case语句可以分为两种类型:

    1. 一种是简单的case语句,它给出一个表达式,并把表达式结果同提供的几个可预见的结果作比较,如果比较成功,则执行对应的语句序列。
    2. 另一种是搜索式的case语句。它会提供多个布尔表达式,然后选择第一个为true的表达式,执行对应的脚本。
      1.简单case语句

        简单case的语法如下:

      create or replace procedure test_case_procedure(idnum varchar)
      as
      v_ly_ds ly_ds%rowtype;
      begin
      select * into v_ly_ds from ly_ds where id=idnum;
      case v_ly_ds.ly_nb
      when '' then
      dbms_output.put_line('女人');
      when '' then
      dbms_output.put_line('男人');
      else
      dbms_output.put_line('人妖');
      end case;
      end;

      分别输入 1 、2 调用后的结果如下:

      匿名块已完成
      男人
      
      匿名块已完成
      女人

      从上边来看,case 的简单语法如下:

      case 标志
      when 参数1 then
      语句块1;
      when 参数2 then
      语句块2;
      else
      语句块3;
      end case;

      执行过程中是,从上到下,依次拿when 后边的参数,分别与上边的标志 进行比较,相等 则进入。
        另外,上边的else 如果不写,假如没有符合条件的when ,则会抛出CASE_NOT_FOUND 异常。

      2.搜索式case语句

        搜索式case与简单式差不多,只不过是搜索式的这种,case后边是没有参数的。
      将上边的例子,修改后如下,即是搜索式case:

      create or replace procedure test_case_procedure(idnum varchar)
      as
      v_ly_ds ly_ds%rowtype;
      begin
      select * into v_ly_ds from ly_ds where id=idnum;
      case 
      when  v_ly_ds.ly_nb='' then
      dbms_output.put_line('女人');
      when  v_ly_ds.ly_nb='' then
      dbms_output.put_line('男人');
      else
      dbms_output.put_line('人妖');
      end case;
      end;
       Merger_YLD_VAL := trunc((aoi_num/gross_num)*100);
       case 
       when Merger_YLD_VAL < 5 then
          update mes_component_attributes ab 
                   set ab.value = 'C' where exists(
                   select 1 from mes_component cm where cm.sysid = ab.fromid and cm.componentid = waferId and ab.name = 'COWGrade');
       when Merger_YLD_VAL >= 5 AND Merger_YLD_VAL <10 then
          update mes_component_attributes ab 
                   set ab.value = 'B' where exists(
                   select 1 from mes_component cm where cm.sysid = ab.fromid and cm.componentid = waferId and ab.name = 'COWGrade');
       when Merger_YLD_VAL >= 10 then
           update mes_component_attributes ab 
                   set ab.value = 'A' where exists(
                   select 1 from mes_component cm where cm.sysid = ab.fromid and cm.componentid = waferId and ab.name = 'COWGrade');
       else
          update mes_component_attributes ab 
                   set ab.value = '' where exists(
                   select 1 from mes_component cm where cm.sysid = ab.fromid and cm.componentid = waferId and ab.name = 'COWGrade');
      commit;
      end case;

      执行过程与上边相同。
        从上边来看,搜索式的case语法如下:

      case 
      when 条件1 then
      语句块1;
      when 条件2 then
      语句块2;
      else
      语句块3;
      end case;

      执行过程中是,从上到下,依次看when 后边的条件是否为truetrue 则进入。
        另外,上边的else 如果不写,假如没有符合条件的when ,也会抛出CASE_NOT_FOUND 异常。

    本文来自博客园,作者:云辰,转载请注明原文链接:https://www.cnblogs.com/yunchen/p/15718366.html

  • 相关阅读:
    Linux下基于PAM机制的USB Key的制作
    IP-route管理路由
    Linux下CPU主板监控工具lm_sensors
    两个网卡隔离方法
    关机后内存的数据是怎么丢失的呢?
    6.0 移动端页面布局
    CyberPlayer 使用教程
    5.10 HTML5 音频和视频
    让Ecshop网店系统用户自动登陆
    設定 Bootstrap/SASS/Bower/gulp (Windows平台)
  • 原文地址:https://www.cnblogs.com/yunchen/p/15718366.html
Copyright © 2020-2023  润新知