您好,欢迎访问一九零五行业门户网

Oracle PL/SQL基础 选择(IF CASE) 、 循环(LOOP WHILE FOR)

一、pl/sql的控制结构,包括选择结构、循环结构和跳转结构
一、pl/sql的控制结构,包括选择结构、循环结构和跳转结构
相关阅读:
rlwrap - 解决linux下sqlplus退格、上翻键乱码问题
sqlplus spool 到动态日志文件名
oracle sqlplus提示符设置
通过设置sqlplus arraysize(行预取)加快sql返回速度
1、选择结构
(1)if语句
if condition1 then  statement1 ;
[ elsif condition2 then statement2 ; ]
...
[ else else_statements ; ]
end if;
attention:pl/sql的逻辑运算结果有 true 、 false 、null 三种情况,所以在进行条件选择判断时,要考虑条件结果为null的情况。
example:
create or replace procedure detector_plsql_if(
      v_control_type in varchar2)
as
    v_pre_print varchar2(100) := 'value of parameter [in] v_control_type';
begin
    if v_control_type = 'if' then
      dbms_output.put_line(v_pre_print || ' (if) :' || v_control_type);
    elsif v_control_type = 'case' then
      dbms_output.put_line(v_pre_print || ' (case) :' || v_control_type);
    elsif v_control_type is null then
      dbms_output.put_line(v_pre_print || ' (null) :' || v_control_type);
    else
      dbms_output.put_line(v_pre_print || ' :' || v_control_type);
    end if;
end detector_plsql_if;
(2)、case 语句
case语句有两种形式:一种只进行等值比较,另一种可以进行多各种条件比较。
a、只进行等值比较
case test_value
 when value1 then statement1;
[ when value2 then statement2; ]
...
[ else else_statement ; ]
end case;
example:
create or replace procedure detector_plsql_case(
  v_control_type in varchar2)
is
  v_pre_print varchar2(100) := 'value of parameter [in] v_control_type';
begin
    case v_control_type
      when 'if' then
          dbms_output.put_line(v_pre_print || ' (if) :' || v_control_type);
      when 'case' then
          dbms_output.put_line(v_pre_print || ' (case) :' || v_control_type);
      else
          dbms_output.put_line(v_pre_print || ' :' || v_control_type);
    end case;
end detector_plsql_case;
b、进行多种条件的比较
case
        when condition1 then statement1 ;
        [ when condition2 then statement2; ]
        ...
        [ else else_statements ; ]
end case;
case语句对每一个when条件进行判断,当条件为真时,执行其后面的语句;如果所有条件都不为真,则执行else后的语句。
attention:在case语句中,当第一个when条件为真时,执行其后的操作,操作完成后结束case语句,其它的when条件不再判断,,其后的操作也不执行。
example:
create or replace procedure detector_plsql_case2(
 v_control_type in varchar2)
as
  v_pre_print varchar2(100) := 'value of parameter [in] v_control_type';
begin
  case
    when v_control_type = 'if' then
        dbms_output.put_line(v_pre_print || ' (if) :' || v_control_type);
    when v_control_type = 'case' then
          dbms_output.put_line(v_pre_print || ' (case) :' || v_control_type);
    when v_control_type is null then
          dbms_output.put_line(v_pre_print || ' (null) :' || v_control_type);
    else
          dbms_output.put_line(v_pre_print || ' :' || v_control_type);     
  end case;
end detector_plsql_case2;
更多详情见请继续阅读下一页的精彩内容:
其它类似信息

推荐信息