Oracle PLSQL程序控制结构

 

Sql代码 复制代码
  1. declare  
  2.    n_empno employee.empno%type;   
  3.    v_name employee.name%type;   
  4.    n_salary employee.salary%type;   
  5.    v_temp varchar2(30);   
  6.    n_temp number(5) := 1;   
  7.    -- 自定义异常   
  8.    e_exception exception;  
declare
   n_empno employee.empno%type;
   v_name employee.name%type;
   n_salary employee.salary%type;
   v_temp varchar2(30);
   n_temp number(5) := 1;
   -- 自定义异常
   e_exception exception;


exception_init 是一个编译时指令,用于将一个内部错误与异常的名称关联。
一旦关联成功后,我们就可以通过名称抛出异常并用when处理器捕获错误

Sql代码 复制代码
  1. pragma exception_init(e_exception,-66666);   
  2.   
  3. in  
  4. n_empno := &员工编号:;   
  5. select name,salary into v_name,n_salary from employee where empno=n_empno;  
   pragma exception_init(e_exception,-66666);
   
begin
   n_empno := &员工编号:;
   select name,salary into v_name,n_salary from employee where empno=n_empno;


if 语句
简单条件判断

Sql代码 复制代码
  1. if n_salary < 4000 then  
  2.    update employee set salary = n_salary+500 where empno=n_empno;   
  3.    dbms_output.put_line(v_name || '的工资增加了500元!');   
  4. else  
  5.    dbms_output.put_line(v_name || '的工资大于4000,不用加工资!');   
  6. end if;   
  7. -- 多重条件分支   
  8. if n_salary < 4000 then  
  9.    dbms_output.put_line(v_name || '的工资少于4000元!');   
  10. elsif n_salary >= 4000 and n_salary < 5000 then  
  11.    dbms_output.put_line(v_name || '的工资在4000~5000元之间!');   
  12. elsif n_salary >= 5000 and n_salary < 6000 then  
  13.    dbms_output.put_line(v_name || '的工资在5000~6000元之间!');   
  14. else  
  15.    dbms_output.put_line(v_name || '的工资大于6000元!');     
  16. end if;  
   if n_salary < 4000 then
      update employee set salary = n_salary+500 where empno=n_empno;
      dbms_output.put_line(v_name || '的工资增加了500元!');
   else
      dbms_output.put_line(v_name || '的工资大于4000,不用加工资!');
   end if;
   -- 多重条件分支
   if n_salary < 4000 then
      dbms_output.put_line(v_name || '的工资少于4000元!');
   elsif n_salary >= 4000 and n_salary < 5000 then
      dbms_output.put_line(v_name || '的工资在4000~5000元之间!');
   elsif n_salary >= 5000 and n_salary < 6000 then
      dbms_output.put_line(v_name || '的工资在5000~6000元之间!');
   else
      dbms_output.put_line(v_name || '的工资大于6000元!');  
   end if;


case 语句
使用单一选择符进行等值比较

Sql代码 复制代码
  1. case n_salary   
  2.    when 4200 then  
  3.       dbms_output.put_line(v_name || '的工资是4200元!');   
  4.    when 3500 then  
  5.       dbms_output.put_line(v_name || '的工资是3500元!');   
  6.    when 6800 then  
  7.       dbms_output.put_line(v_name || '的工资是6800元!');   
  8.    else  
  9.       dbms_output.put_line(v_name || '的工资是' || n_salary ||'元!');   
  10.  end case;   
  11.  -- 使用多条件比较   
  12.  case  
  13.     when n_salary < 4000 then  
  14.          dbms_output.put_line(v_name || '的工资少于4000元!');   
  15.     when n_salary < 5000 then  
  16.          dbms_output.put_line(v_name || '的工资少于5000元!');   
  17.     when n_salary < 6000 then  
  18.          dbms_output.put_line(v_name || '的工资少于6000元!');     
  19.     else  
  20.          dbms_output.put_line(v_name || '的工资大于6000元!');   
  21.  end case;  
   case n_salary
      when 4200 then
         dbms_output.put_line(v_name || '的工资是4200元!');
      when 3500 then
         dbms_output.put_line(v_name || '的工资是3500元!');
      when 6800 then
         dbms_output.put_line(v_name || '的工资是6800元!');
      else
         dbms_output.put_line(v_name || '的工资是' || n_salary ||'元!');
    end case;
    -- 使用多条件比较
    case
       when n_salary < 4000 then
            dbms_output.put_line(v_name || '的工资少于4000元!');
       when n_salary < 5000 then
            dbms_output.put_line(v_name || '的工资少于5000元!');
       when n_salary < 6000 then
            dbms_output.put_line(v_name || '的工资少于6000元!');  
       else
            dbms_output.put_line(v_name || '的工资大于6000元!');
    end case;


case 表达式
使用单一选择符

Sql代码 复制代码
  1. v_temp := case n_salary   
  2.               when 3500 then v_name || '的工资是3500元!'  
  3.               when 4200 then v_name || '的工资是4200元!'  
  4.               when 6800 then v_name || '的工资是6800元!'  
  5.           else  
  6.               v_name || '的工资是' || n_salary ||'元!'  
  7.           end;   
  8. dbms_output.put_line('v_temp:' || v_temp);   
  9. -- 使用多条件比较   
  10. v_temp := case  
  11.              when n_salary < 4000 then 4000   
  12.              when n_salary < 5000 then 5000   
  13.              when n_salary < 6000 then 6000   
  14.              else  
  15.                   n_salary   
  16.           end + 500;   
  17.  dbms_output.put_line(v_name || '的工资原工资是' || n_salary || ' 调整工资并增加500元奖金后的结算工资是 ' || v_temp);                       
  18.  -- 将case 语句用在SQL语句中   
  19.  select case    
  20.         when manager = 1 then '经理'  
  21.         else '职员'  
  22.         -- 是经理还是职员 表示列的别名,也可以不写,在这里不要打引号   
  23.         end 是经理还是职员 into v_temp    
  24.         from employee where empno = n_empno;   
  25.  dbms_output.put_line(v_name || '是:' || v_temp);  
    v_temp := case n_salary
                  when 3500 then v_name || '的工资是3500元!'
                  when 4200 then v_name || '的工资是4200元!'
                  when 6800 then v_name || '的工资是6800元!'
              else
                  v_name || '的工资是' || n_salary ||'元!'
              end;
    dbms_output.put_line('v_temp:' || v_temp);
    -- 使用多条件比较
    v_temp := case
                 when n_salary < 4000 then 4000
                 when n_salary < 5000 then 5000
                 when n_salary < 6000 then 6000
                 else
                      n_salary
              end + 500;
     dbms_output.put_line(v_name || '的工资原工资是' || n_salary || ' 调整工资并增加500元奖金后的结算工资是 ' || v_temp);                    
     -- 将case 语句用在SQL语句中
     select case 
            when manager = 1 then '经理'
            else '职员'
            -- 是经理还是职员 表示列的别名,也可以不写,在这里不要打引号
            end 是经理还是职员 into v_temp 
            from employee where empno = n_empno;
     dbms_output.put_line(v_name || '是:' || v_temp);


循环控制
基本循环

Sql代码 复制代码
  1. loop   
  2.    -- 程序在这里至少会执行一次,注意一定要加上exit when……结束循环的条件,否则程序会进入死循环   
  3.    exit when n_temp >= 100;   
  4.    n_temp := n_temp + 1;   
  5. end loop;   
  6. dbms_output.put_line('n_temp loop循环100次累加的结果是:' || n_temp);   
  7. -- while 循环   
  8. while n_temp <= 100 loop   
  9.    n_temp := n_temp + 1;   
  10. end loop;   
  11. dbms_output.put_line('n_temp while循环100次累加的结果是:' || n_temp);  
     loop
        -- 程序在这里至少会执行一次,注意一定要加上exit when……结束循环的条件,否则程序会进入死循环
        exit when n_temp >= 100;
        n_temp := n_temp + 1;
     end loop;
     dbms_output.put_line('n_temp loop循环100次累加的结果是:' || n_temp);
     -- while 循环
     while n_temp <= 100 loop
        n_temp := n_temp + 1;
     end loop;
     dbms_output.put_line('n_temp while循环100次累加的结果是:' || n_temp);


for 循环
如果指定了reverse 选项,那么每次循环时变量会递减,否则变量会递加,注意循环中用的是两个"."

Sql代码 复制代码
  1. for i in reverse 1..100 loop   
  2.     dbms_output.put_line('for循环中I的值:' || i);   
  3. end loop;   
  4. -- 嵌套循环和标号(Lable)   
  5. -- 定义一个标号   
  6. <<outer>>   
  7. for i in 1..100 loop   
  8.     <<inner>>   
  9.     for j in 1..100 loop   
  10.         n_temp := i*j;   
  11.         -- 如果n_temp=1000 就直接退出外层循环   
  12.         exit outer when n_temp = 1000;   
  13.         -- 如果n_temp=500 只会退出内层循环   
  14.         exit inner when n_temp = 500;   
  15.     end loop inner;        
  16.     dbms_output.put_line('内层:' || n_temp);   
  17. end loop outer;   
  18. dbms_output.put_line('外层:' || n_temp);  
     for i in reverse 1..100 loop
         dbms_output.put_line('for循环中I的值:' || i);
     end loop;
     -- 嵌套循环和标号(Lable)
     -- 定义一个标号
     <<outer>>
     for i in 1..100 loop
         <<inner>>
         for j in 1..100 loop
             n_temp := i*j;
             -- 如果n_temp=1000 就直接退出外层循环
             exit outer when n_temp = 1000;
             -- 如果n_temp=500 只会退出内层循环
             exit inner when n_temp = 500;
         end loop inner;     
         dbms_output.put_line('内层:' || n_temp);
     end loop outer;
     dbms_output.put_line('外层:' || n_temp);


顺序控制
goto 语句用于跳转到特定标号处去执行语句,一般不建议使用

Sql代码 复制代码
  1. for i in 1..100 loop   
  2.     if i > 50 then  
  3.        goto end_loop;   
  4.     end if;   
  5.     dbms_output.put_line(i);   
  6. end loop;    
  7. <<end_loop>>   
  8. dbms_output.put_line('loop 循环了50次提前结束了!' );   
  9. -- NULL 语句,它不会执行任何操作,并且会将控制直接传到下一条语句   
  10. if n_salary > 5000 then  
  11.    dbms_output.put_line(v_name || '的工资大于4000');   
  12. else    
  13.    null;   
  14. end if;  
     for i in 1..100 loop
         if i > 50 then
            goto end_loop;
         end if;
         dbms_output.put_line(i);
     end loop; 
     <<end_loop>>
     dbms_output.put_line('loop 循环了50次提前结束了!' );
     -- NULL 语句,它不会执行任何操作,并且会将控制直接传到下一条语句
     if n_salary > 5000 then
        dbms_output.put_line(v_name || '的工资大于4000');
     else 
        null;
     end if;


异常处理
在要处理抛出的异常的异常处理部分的when子句中引用raise e_exception;
用raise_application_error替代raise ,我们可以将错误消息与异常关联在一起
错误号必须是在-20000到-20999之间的负整数;错误描述的长度不能超过2048字节;
第三个参数为可选参数,如果设置为true,则该错误号被放在先前的错误堆栈中;
如果设置为false(默认值),则会替换先前所有错误

Sql代码 复制代码
  1.      raise_application_error(-20001,'这里有Application异常!',true);   
  2. exception   
  3.      -- 系统异常   
  4.      when NO_DATA_FOUND then dbms_output.put_line(sqlerrm);   
  5.      -- 自定义异常   
  6.      /*   
  7.         异常名只能在两个地方引用:   
  8.         一、在要抛出异常的程序执行部分用RAISE语句引用:如:raise e_exception;   
  9.         二、在要处理抛出的异常的异常处理部分的when子句中引用   
  10.      */   
  11.      when e_exception then dbms_output.put_line('这里是自定义异常:' || sqlerrm);   
  12.      /*   
  13.         使用异常函数:   
  14.         函数sqlcode用于取得Oracle错误号,而sqlerrm则用于取得与之相关的错误消息   
  15.      */   
  16.      when others then dbms_output.put_line('错误号:' || sqlcode || '   错误消息:' || sqlerrm);   
  17. end;   
  18. /  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值