ITPub博客

首页 > 数据库 > 数据库开发技术 > 学习《PLSQL开发指南》笔记——条件和序列控制

学习《PLSQL开发指南》笔记——条件和序列控制

原创 数据库开发技术 作者:genweihua 时间:2014-11-02 17:47:18 0 删除 编辑

一、三种条件语句
1、if-then-end if
2、if-then-else-end if
2、if-then-elsif-then-else-end if
注意:1、条件是boolean型,null和任何类型的运算都是false;
      2、来自数据库的值,要关注null问题:is null,nvl;
      3、多条件,也需要排除null值,and或者or,只需要其中一个条件true或者false,就可以判断结果。
      begin
        if null or null
          then
            dbms_output.put_line('null值的运算结果是true!');
        else
          dbms_output.put_line('null值得运算结果是false!');
        end if;
        end;
二、case 语句和case表达式
    1、case语句
    declare
      v_case varchar2(10) := 'ding';
    begin
      case v_case
        when 'd' then
          dbms_output.put_line('选中的字母是:' || v_case);
        when 'ding' then
          dbms_output.put_line('选中的字母是:' || v_case);
        else
          dbms_output.put_line('没有选中字母');
      end case;
    end;
    2、case 语句
    declare
      v_case varchar2(10) := 'd';
    begin
      case
        when v_case = 'd' then
          dbms_output.put_line('选中的字母是:' || v_case);
        when v_case = 'ding' then
          dbms_output.put_line('选中的字母是:' || v_case);
        else
          dbms_output.put_line('没有选中字母');
      end case;
    end;
    3、case 语句
    declare
      v_num int := 1500;
    begin
      case
        when v_num >= 1000 then
          case
            when v_num >= 1200 then
              dbms_output.put_line('传入的数据大于1200!');
            when v_num >= 1500 then
              dbms_output.put_line('传入的数据大于1500!');
            when v_num >= 4000 then
              dbms_output.put_line('传入的数据大于4000!');
          end case;
          when v_num < 1000 then dbms_output.put_line('传入的值小于1000!');
      end case;
      end;
     4、case 表达式:转换结果,可以给函数传参数:case when then else end;
     declare
       t_bl boolean := true;
       f_bl boolean := false;
       n_bl boolean;
    
       function v_func(v_flag in boolean) return varchar2 is
       begin
         return case v_flag when true then 'True' when false then 'Else' else 'null' end;
       end;
     begin
       dbms_output.put_line(v_func(t_bl));
       dbms_output.put_line(v_func(f_bl));
       dbms_output.put_line(v_func(n_bl));
     end;
case 表达式和case语句的区别
  1、case 语句以end case 结尾;case 表达式以end结尾;
  2、case 语句then 以后有分号;case 表达式没有;
  3、case 语句中没有else 会报错误;case表达式没有else则返回null;
  4、case 表达式以end结尾可以参与到运算中;
最后的例子:
declare
  dump_mesg varchar2(20);
begin
  select case
           when dummy = 'X' then
            'dual is OK!'
           else
            'dual is messed up!'
         end
    into dump_mesg
    from dual;

  dbms_output.put_line(dump_mesg);
end;
      

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/22275400/viewspace-1317918/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 业务运维
请登录后发表评论 登录
全部评论
事了拂衣去,深藏功与名!

注册时间:2009-08-28

  • 博文量
    114
  • 访问量
    623627