PL/SQL 03 流程控制

--IF语法
IF condition THEN
  statements;
[ELSIF condition THEN
  statements;]
[ELSE
  statements;]
END IF;


--CASE 语法

1、在 CASE 语句中使用单一选择符进行等值比较

CASE selector
  WHEN expression1 THEN sequence_of_statements1;
  WHEN expression2 THEN sequence_of_statements2;
  ...
  WHEN expressionN THEN sequence_of_statementsN;
  [ELSE sequence_of_statementsN+1;]
END CASE;


2、在 CASE 语句中使用多种条件比较

CASE
  WHEN search_condition1 THEN sequence_of_statements1;
  WHEN search_condition2 THEN sequence_of_statements2;
  ...
  WHEN search_conditionN THEN sequence_of_statementsN;
  [ELSE sequence_of_statementsN+1;]
END CASE;


--循环


1、基本循环 LOOP

LOOP
  statement1;
  ...
  EXIT [WHEN condition];
END LOOP;


2、WHILE 循环

WHILE condition LOOP
  statement1;
  statement2;
  ...
END LOOP;


3、FOR 循环

FOR counter in [REVERSE]
lower_bound..upper_bound LOOP
  statement1;
  statement2;
  ...
END LOOP;


4、嵌套循环和标号

嵌套循环是指在一个循环语句之中嵌入另一个循环语句,而标号(Label)则用于标记嵌套块或嵌套循环。通过在嵌套循环中使用标号,可以区分内层循环和外层循环,并且可以在内层循环中直接退出外层循环。在编写 PL/SQL 块时,可以使用<<label_name>>定义标号。


declare
  result int;
begin
<>
  for i in 1..100 loop
  <>
  for j in 1..100 loop
  result:=i*j;
  exit outer when result=1000;
  exit when result=500;
  end loop inner;
  dbms_output.put_line(result);
  end loop outer;
  dbms_output.put_line(result);
end;
/


如上所示,当执行 PL/SQL 块时,如果 result=1000,那么会直接退出外层循环,而当 result=500 时只会退出内层循环。


--顺序控制语句 GOTO 、NULL

PL/SQL 不仅提供了条件分支语句和循环控制语句,而且还提供了顺序控制语句 GOTO 和 NULL。但与 IF,CASE 和 LOOP 语句不同,GOTO 语句和 NULL 语句,一般情况下不要用。


1、GOTO

GOTO 语句用于跳转到特定标号处去执行语句。注意,以为使用 GOTO 语句会增加程序的复杂性,并且使得应用程序可读性差,所以开发应用程序时,一般都建议用户不要使用 GOTO 语句。

其语法如下:

GOTO label_name;

其中,label_name 是已经定义的标号名。注意,当使用 GOTO 跳转到特定标号时,标号后至少要包含一条可执行语句。示例如下:

declare
  i int:=1;
begin
  loop
  insert into temp values(i);
  if i=10 then
    goto end_loop;
  end if;
  i:=i+1;
  end loop;
<<end_loop>>
  dbms_output.put_line('循环结束');
end;
/

如上所示,在执行以上 PL/SQL 块时,如果 i=10,则会跳转到标号 end_loop,并执行随后的语句。


2、NULL

NULL语句不会执行任何操作,并且会直接将控制传递到下一条语句。使用 NULL 语句的主要好处是可以提高 PL/SQL 程序的可读性,示例如下:

declare
  v_sal emp.sal%type;
  v_ename emp.ename%type;
begin
  select ename,sal into v_ename,v_sal from emp where empno='&no';
  if v_sal<3000 then
    update emp set comm=sal*0.1 where ename=v_ename;
  else
    null;
  end if;
end;
/

当执行以上 PL/SQL 块时,会根据输入的雇员号确定雇员名及其工资;如果雇员工资低于3000,则将其补助设置为工资的10%;如果雇员的工资高于3000,则不会执行任何操作(NULL)。



--示例

declare
  cj xkb.chengji%type;
  state varchar2(4);
begin
  select chengji into cj from xkb
  where stuid=101 and kcid=1001;
  if cj between 90 and 100 then                         --if语句
    state:='优秀';
  elsif cj between 70 and 89 then
    state:='良好'; 
  elsif cj between 60 and 69 then
    state:='及格';
  else
    state:='差';
  end if;
  dbms_output.put_line('分数是:'||cj);
  dbms_output.put_line('等级是:'||state);
end;

select stuname,sex,
       case sex                                         --case 值的比较
         when 'm' then '男'
         when 'f' then '女'
         else '默认值'
       end "性别"
from stu

IKKI@ test10g> declare
  2  v_deptno emp.deptno%type;
  3  begin
  4    v_deptno:='&no';
  5    case v_deptno
  6    when 10 then
  7      update emp set comm=100 where deptno=v_deptno;
  8    when 20 then
  9      update emp set comm=80 where deptno=v_deptno;
 10    when 30 then
 11      update emp set comm=50 where deptno=v_deptno;
 12    else
 13      dbms_output.put_line('not found');
 14    end case;
 15  end;
 16  /
Enter value for no: 10
old   4:   v_deptno:='&no';
new   4:   v_deptno:='10';

PL/SQL procedure successfully completed.


select stuname,sex,                                    --decode 值的比较(不能条件判断)
       decode(sex,
         'm','男',
         'f','女',
         '默认值') "性别"
from stu


select stuid,kcid,
       case                                             --case 条件判断
         when chengji between 90 and 100 then '优秀'
         when chengji between 70 and 89 then '良好'
         when chengji between 60 and 69 then '及格'
         else '差'
       end  state 
from xkb

IKKI@ test10g> declare
  2  v_sal emp.sal%type;
  3  v_ename emp.ename%type;
  4  begin
  5    select ename,sal into v_ename,v_sal
  6    from emp where empno='&no';
  7    case
  8    when v_sal<1000 then
  9      update emp set comm=100 where ename=v_ename;
 10    when v_sal<2000 then
 11      update emp set comm=80 where ename=v_ename;
 12    when v_sal<6000 then
 13      update emp set comm=50 where ename=v_ename;
 14    end case;
 15  end;
 16  /
Enter value for no: 7788
old   6:   from emp where empno='&no';
new   6:   from emp where empno='7788';

PL/SQL procedure successfully completed.

declare
  m int:=0;
  n int:=1;
begin
  loop                                 --loop循环
    m:=m+n;
    n:=n+1;
    exit when n=101;
   end loop;
   dbms_output.put_line('m is :'||m);
end;


declare
  m int:=0;
  n int:=1;
begin
  while n<=100                         --while循环
  loop
    m:=m+n;
    n:=n+1;
  end loop;
   dbms_output.put_line('m is :'||m);
end;


declare
  m int:=0;
  n int;
begin
  for n in 1..100                      --for循环
  loop
    m:=m+n;
  end loop;
   dbms_output.put_line('m is :'||m);
end;

原文地址:https://www.cnblogs.com/john2017/p/6364496.html