zoukankan      html  css  js  c++  java
  • 04-异常处理机制


      1 --[预定义异常]
      2 declare
      3 
      4   v_sal employees.salary%type;
      5 begin
      6   select salary into v_sal
      7   from employees
      8   where employee_id >100;
      9   
     10   dbms_output.put_line(v_sal);
     11 
     12 exception
     13   when Too_many_rows then dbms_output.put_line('输出的行数太多了');
     14 end;
     15 
     16 --[非预定义异常]
     17 declare
     18 
     19   v_sal employees.salary%type;
     20   --声明一个异常
     21   delete_mgr_excep exception;
     22   --把自定义的异常和oracle的错误关联起来
     23   PRAGMA EXCEPTION_INIT(delete_mgr_excep,-2292);
     24 begin
     25   delete from employees
     26   where employee_id = 100;
     27   
     28   select salary into v_sal
     29   from employees
     30   where employee_id >100;
     31   
     32   dbms_output.put_line(v_sal);
     33 
     34 exception
     35   when Too_many_rows then dbms_output.put_line('输出的行数太多了');
     36   when delete_mgr_excep then dbms_output.put_line('Manager不能直接被删除');
     37 end;
     38 
     39 --[用户自定义异常]
     40 declare
     41 
     42   v_sal employees.salary%type;
     43   --声明一个异常
     44   delete_mgr_excep exception;
     45   --把自定义的异常和oracle的错误关联起来
     46   PRAGMA EXCEPTION_INIT(delete_mgr_excep,-2292);
     47   
     48   --声明一个异常
     49   too_high_sal exception;
     50 begin
     51 
     52   select salary into v_sal
     53   from employees
     54   where employee_id =100;
     55   
     56   if v_sal > 1000 then
     57      raise too_high_sal;
     58   end if;
     59      
     60   delete from employees
     61   where employee_id = 100;
     62 
     63   dbms_output.put_line(v_sal);
     64 
     65 exception
     66   when Too_many_rows then dbms_output.put_line('输出的行数太多了');
     67   when delete_mgr_excep then dbms_output.put_line('Manager不能直接被删除');
     68   --处理异常
     69   when too_high_sal then dbms_output.put_line('工资过高了');
     70 end;
     71 
     72 --18. 异常的基本程序: 
     73 通过 select ... into ... 查询某人的工资, 若没有查询到, 则输出 "未找到数据"
     74 
     75 declare
     76   --定义一个变量
     77   v_sal employees.salary%type;
     78 begin
     79   --使用 select ... into ... 为 v_sal 赋值
     80   select salary into v_sal from employees where employee_id = 1000;
     81   dbms_output.put_line('salary: ' || v_sal);
     82 exception
     83   when No_data_found then 
     84        dbms_output.put_line('未找到数据');
     85 end;
     86 
     87  88 
     89 declare
     90   --定义一个变量
     91   v_sal employees.salary%type;
     92 begin
     93   --使用 select ... into ... 为 v_sal 赋值
     94   select salary into v_sal from employees;
     95   dbms_output.put_line('salary: ' || v_sal);
     96 exception
     97   when No_data_found then 
     98        dbms_output.put_line('未找到数据!');
     99   when Too_many_rows then 
    100        dbms_output.put_line('数据过多!');     
    101 end;
    102 
    103 --19. 更新指定员工工资,如工资小于300,则加100;对NO_DATA_FOUND 异常, TOO_MANY_ROWS 进行处理.
    104 declare
    105    v_sal employees.salary%type;
    106 begin
    107    select salary into v_sal from employees where employee_id = 100;
    108    
    109    if(v_sal < 300) then update employees set salary = salary + 100 where employee_id = 100;
    110    else dbms_output.put_line('工资大于300');
    111    end if;
    112 exception
    113    when no_data_found then dbms_output.put_line('未找到数据');
    114     when too_many_rows then dbms_output.put_line('输出的数据行太多');
    115 end;
    116 
    117 --20. 处理非预定义的异常处理: "违反完整约束条件"
    118 
    119 declare
    120   --1. 定义异常    
    121   temp_exception exception;
    122 
    123   --2. 将其定义好的异常情况,与标准的 ORACLE 错误联系起来,使用 EXCEPTION_INIT 语句
    124   PRAGMA EXCEPTION_INIT(temp_exception, -2292);
    125 begin
    126   delete from employees where employee_id = 100;
    127 
    128 exception
    129   --3. 处理异常
    130   when temp_exception then
    131        dbms_output.put_line('违反完整性约束!');
    132 end;
    133 
    134 --21. 自定义异常: 更新指定员工工资,增加100;若该员工不存在则抛出用户自定义异常: no_result
    135 
    136 declare
    137   --自定义异常                                   
    138   no_result exception;   
    139 begin
    140   update employees set salary = salary + 100 where employee_id = 1001;
    141 
    142   --使用隐式游标, 抛出自定义异常
    143   if sql%notfound then
    144      raise no_result;
    145   end if;  
    146 
    147 exception
    148 
    149   --处理程序抛出的异常
    150   when no_result then
    151      dbms_output.put_line('更新失败');
    152 end;

  • 相关阅读:
    powerdesigner 设置自动增长列(identity)和默认值
    Powercenter体系结构和主要组件介绍
    Asp.net和C# 函数方法 (2)【转载】
    .NET和C# 函数方法(一)
    sql 删除数据库的时候注意
    使用PowerDesigner建立数据库模型【转】
    c#获取当前日期时间
    在windows平台安装Informatica PowerCenter【转载】
    Informatica Powercenter 介绍
    3、对变量在栈上存储顺序,及函数返回值与参数在栈上存放顺序的思考(1)
  • 原文地址:https://www.cnblogs.com/shici/p/14406610.html
Copyright © 2011-2022 走看看