PL/SQL流程控制(原创)

概述

类似于高级语言,流程控制语句是PL/SQL语言的重要组成部分。这些流程控制语句使得PL/SQL加大了代码的灵活性和多样性,大大简化了程序的编写。下面将列出流程控制语句并给出具体事例。

条件分支结构if

IF ... THEN ... END IF
  IF condition THEN
      statement
  END IF;
判断condition是否成立,成立就执行IF 与END IF 之间的语句。
例:输入员工编号,查询其工资,如果他们的职位是CLERK,则工资增加%,再显示修改前后的工资数。

declare
    v_empid emp.empno%type;
    v_job   emp.job%type;
    v_old_sal emp.sal%type;
    v_new_sal emp.sal%type;
begin
    v_empid := &inputid;
    select job,sal into v_job,v_old_sal from emp where empno=v_empid;
    if v_job='CLERK' then
      v_new_sal := v_old_sal * 1.1;
      update emp set sal = v_new_sal where empno=v_empid;
      dbms_output.put_line('Old sal: '|| v_old_sal);
      dbms_output.put_line('New sal: '|| v_new_sal);
    end if;
exception
    when no_data_found then
       dbms_output.put_line('NOT FOUND RECORD');
end;

注意:PL/SQL代码中依然需要显示的指定提交或回滚才能结束事务。上面的代码并没有结束事务

=============================================================


IF ... THEN ... ELSE ... END IF
IF condition THEN
    statements1;
ELSE
    statements2;
END IF;
判断condition是否成立,成立就执行IF 与ELSE 之间的语句,
否则执行ELSE 与END IF之间的语句。
例:输入员工编号,查询其工资,如果他们的职位是CLERK,则工资增加10%,如果不是CLERK,工资增加20%,再显示修改前后的工资数。
declare
     v_empid emp.empno%type;
     v_job   emp.job%type;
     v_old_sal emp.sal%type;
     v_new_sal emp.sal%type;
begin
     v_empid := &inputid;
     select job,sal into v_job,v_old_sal from emp where empno=v_empid;
     if v_job = 'CLERK' THEN
       v_new_sal := v_old_sal * 1.1;
     else
       v_new_sal := v_old_sal * 1.2;
     end if;

       update emp set sal = v_new_sal where empno = v_empid;
       dbms_output.put_line(' The staff' || '''' || 's job is '|| v_job || ' and his old salary is '|| v_old_sal );
       dbms_output.put_line(' The staff' || '''' || 's job is '|| v_job || ' and his new salary is '|| v_new_sal );
end;
=============================================================

 

IF ... THEN ... ELSIF ... THEN ... ELSE ... END IF
IF condition1 THEN
    statements1;
ELSIF condition2 THEN
    statements2;
ELSE
    else_statements;
END IF;
例:输入员工编号,查询其工资,如果其职位是CLERK,则工资增加10%,如果是SALESMAN工资增加20%,其它的加30%,显示修改前后的工资数。
declare
    v_empid emp.empno%type;
    v_job   emp.job%type;
    v_old_sal   emp.sal%type;
    v_new_sal   emp.sal%type;
begin
    v_empid := &intputid;
    select job,sal into v_job,v_old_sal from emp where empno = v_empid;
    if v_job = 'CLERK' THEN
      v_new_sal := 1.1 * v_old_sal;
    elsif v_job = 'CLERK'  THEN
      v_new_sal := 1.2 * v_old_sal;
    else
      v_new_sal := 1.3 * v_old_sal;
    end if;
     update emp set sal = v_new_sal where empno = v_empid;
      dbms_output.put_line('The staff' || '''' || 's job is ' || v_job || ' and his original salary is ' || v_old_sal);
      dbms_output.put_line('The staff' || '''' || 's job is ' || v_job || ' and his new salary is ' || v_new_sal);

exception
    when no_data_found then
      dbms_output.put_line('No Found Record');
end;

=============================================================

 

 等值比较的CASE多分支

 CASE expression
            WHEN result_1 THEN
            statements1;
            WHEN result_2 THEN
            statemnts2;
              ......
            [ELSE
            else_statements;]
        END CASE;
使用case分支完成前面的示例
例:输入员工编号,查询其工资,如果其职位是CLERK,则工资增加10%,如果是SALESMAN工资增加20%,其它的加30%,显示修改前后的工资数。
declare
   v_empid emp.empno%type;
   v_job   emp.job%type;
   v_old_sal emp.sal%type;
   v_new_sal emp.sal%type;
begin
   v_empid := &imputid;
   select sal,job into v_old_sal,v_job from emp where empno = v_empid;
   case v_job
     when 'CLERK' then
       v_new_sal := v_old_sal * 1.1;
     when 'SALESMAN' then
       v_new_sal := v_old_sal * 1.2;
     else
       v_new_sal := v_old_sal * 1.3;
   end case;
   update emp set sal = v_new_sal where empno = v_empid;
   dbms_output.put_line('The staff' || '''' || 's job is ' || v_job || ' and his original salary is ' || v_old_sal);
   dbms_output.put_line('The staff' || '''' || 's job is ' || v_job || ' and his new salary is ' || v_new_sal);
exception
   when no_data_found then
     dbms_output.put_line('Not Record Found');
end;

========================================================

 

件比较的CASE语句
CASE
    WHEN expression_1 THEN
        statements1;
    WHEN expression_2 THEN
        statements2;
    WHEN expression_3 THEN
        statements3;
        ......
    [ELSE
        else_statements;]
END CASE使用case分支完成前面的示例,这里只出示case部分

case
     when v_job = 'CLERK' then
       v_new_sal := v_old_sal * 1.1;
     when v_job = 'SALEMAN' then
       v_new_sal := v_old_sal * 1.2;
     else
       v_new_sal := v_old_sal * 1.3;
end case;

注意,在if和case的条件分支中,条件的选择根据语句编写的顺序顺序执行,故PL\SQL将执行条件分支中第一个满足条件的语句。

如下例中

case
     when v_job = 'CLERK' then
       v_new_sal := v_old_sal * 1.1;
     when v_empno = 7369 then
       v_new_sal := v_old_sal * 1.2;
     else
       v_new_sal := v_old_sal * 1.3;
end case;

这里7369号员工同的工作为'CLERK',v_job='CLERK'较先匹配条件。故他的工资涨幅为10%

======================================================

LOOP循环
LOOP
    statement;
    EXIT [WHEN condition];
END LOOP;
例:用LOOP写一个程序求1++++. . . +100 之和

declare
  n int := 1;
  m int := 0;
begin
  loop
    m := m + n;
    n := n + 1;
    exit when n > 100;
  end loop;
  dbms_output.put_line('The Result : ' || m);
end;

LOOP循环的好处在于可以不指定循环条件,当然这个特性也有可能使其造成死循环。======================================================

WHILE 循环
WHIEL condition LOOP
    statement;
END LOOP;

下面使用while循环完成loop循环中的示例
declare
   n int := 1;
   m int := 0;
begin
   while n <= 100 loop
     m := m + n;
     n := n + 1;
   end loop;
dbms_output.put_line('The Result : ' || m);
end;======================================================

FOR循环
FOR loop_index IN [reverse] lowest_number ..highest_number LOOP
    statements;
END LOOP;
下面使用for循环完成loop循环中的示例

declare
   n int ;
   m int := 0;
begin
   for n in 1..100 loop
     m := m +n;
   end loop;
dbms_output.put_line('The Result : ' || m);
end;

注意:reverse是方向循环,即n从100开始降序到1。无论是否加reverse参数,都应遵守lowest_number ..highest_number的原则

GOTO语句
    GOTO label_name
下面使用goto语句完成loop循环中的示例
 declare
   n int := 1;
   m int :=0;
begin
   loop
     m := m + n;
     n := n + 1;
     if n > 100 then
       goto out;
     end if;
   end loop;
   <<out>>
   dbms_output.put_line('The Result : ' || m);
end;
使用GOTO语句应注意:
标号后至少要有一条语句
PL/SQL块内可以相互跳转,内层块可以跳到外层块,但外层块不能跳到内层块
不能从某一IF语句外部跳到其内部
不能从某一循环外跳到其内部
不能从某一子程序外跳到其内

笔者并不建议使用goto语句进行程序结构的控制,这种看似灵活的编码方案增加了后续维护的工作量和程序的可读性。

NULL
NULL语句不会执行任何操作,并且会直接将控制传递到下一条语句,使用NULL语句主要是提高程序的可读性

例:为工资小于1000的员工涨薪20%

declare
   v_empid   emp.empno%type;
   v_old_sal emp.sal%type;
   v_new_sal emp.sal%type;
begin
   v_empid := &intputid;
   select sal into v_old_sal from emp where empno = v_empid;
   if v_old_sal < 1000 then
      v_new_sal := v_old_sal * 1.2;

   else

      null;
   end if;
   update emp set sal = v_new_sal where empno = v_empid;
   dbms_output.put_line('The New Salary: ' || v_new_sal);
exception
   when no_data_found then
   dbms_output.put_line('Not Record Found');
end;

 

参考至:http://www.cnblogs.com/songsh96/archive/2006/12/28/606549.html
               http://blog.csdn.net/robinson_0612/article/details/6063422
本文原创,转载请注明出处、作者
如有错误,欢迎指正
邮箱:[email protected]



已有 0 人发表留言,猛击->> 这里<<-参与讨论


ITeye推荐
  • —软件人才免语言低担保 赴美带薪读研!—



你可能感兴趣的:(sql,控制,PL)