例外处理【PL/SQL】

  1. 预定义例外【处理常见的Oracle错误】 
    -- no_data_found
    -- 编写一个块,输入雇员的编号,并显示改雇员的姓名
    -- 如果雇员的编号不存在,怎样去处理?
    declare
    v_name varchar2(50);
    begin
      select ename into v_name from emp where empno=&emp_no;
      dbms_output.put_line('雇员名是;'||v_name);
      exception
        when no_data_found then
          dbms_output.put_line('输入的编号不存在!');
    end;
    -- case_not_found
    create or replace procedure case_pro(emp_no number) is
    v_sal emp.sal%type;
    begin
      select sal into v_sal from emp where empno=emp_no;
      case
        when v_sal<1000 then
          update emp set sal=sal+100 where empno=emp_no;
        when v_sal<2000 then
          update emp set sal=sal+200 where empno=emp_no;
      end case;
      exception
          when case_not_found then
            dbms_output.put_line('case语句没有与'||v_sal||'匹配的条件!');
    end;
    -- cursor_already_open
    declare
      -- 定义游标变量
      -- 第二种定义游标的方法
      cursor emp_cursor is select ename,sal from emp;
    begin
      open emp_cursor;--第一次打开游标
      for emp_record in emp_cursor loop--第二次打开游标
        dbms_output.put_line(emp_record.ename);
      end loop;
      exception
        when cursor_already_open then
          dbms_output.put_line('游标已经打开');
    end;
    -- dup_val_on_index
    begin
      insert into dept values(10,'公安部','北京');
      exception
        when dup_val_on_index then
          dbms_output.put_line('在deptno列上不能出现重复值');
    end;
    -- invalid_cursor
    -- 当试图在不合法的游标上执行操作时,会触发该例外
    -- 例如:试图从没有打开的游标提取数据,或是关闭没有打开的游标。则会触发该例外
    declare
      cursor emp_cursor is select ename,sal from emp;
      emp_record emp_cursor%rowtype;
    begin
      open emp_cursor;--打开游标
      fetch emp_cursor into emp_record;
      dbms_output.put_line(emp_record.ename);
      fetch emp_cursor into emp_record;
      dbms_output.put_line(emp_record.ename);
      close emp_cursor;
    exception
      when invalid_cursor then
        dbms_output.put_line('请检查游标是否打开');
    end;
    -- invalid_number
    -- 当输入的数据有误时,会触发该例外
    -- 比如:数字100,写成1oo就会触发该例外
    
    begin
      update emp set sal=sal+'100' where empno=7788;
    exception
      when invalid_number then
        dbms_output.put_line('输入的数字不正确!');
    end;
    -- too_many_rows
    -- 当执行select into语句时,如果返回超过了一行,则会触发该例外
    declare
      v_ename emp.ename%type;
    begin
      select ename into v_ename from emp;
    exception
      when too_many_rows then
        dbms_output.put_line('返回多行!');
    end;
    -- zero_divide
    -- 当执行2/0语句时,则会触发该例外。
    -- value_error
    -- 当在执行赋值操作时,如果变量的长度不足以容纳实际数据,则会触发该例外value_error,比如
    
    declare
      v_ename varchar2(3);
    begin
      select ename into v_ename from emp where empno=&emp_no;
      dbms_output.put_line(v_ename);
    exception
      when value_error then
        dbms_output.put_line('变量尺寸不足');
    end;
    -- 1. 
    -- login_denide
    -- 当用户非法登录时,会触发该例外
    -- 2.
    -- not_logged_on
    -- 如果用户没有登录就执行dml操作,就会触发该例外
    -- 3. 
    -- storage_error
    -- 如果超出了内存空间或是内存被损坏,就触发该例外
    -- 4. 
    -- 如果Oracle在等待资源时,出现了超时就触发该例外

     

  2. 非预定义例外【处理预定义例外不能处理的例外】
  3. 自定义例外【处理与Oracle错误无关的其它情况】
    -- 编写一个块,接收一个雇员的编号,并给该雇员工资增加1000,
    -- 如果该雇员不存在,请提示。
    
    -- 自定义例外
    -- 这里是update和之前的select的no_data_found是不一样的
    create or replace procedure ex_test(emp_no number) is
    -- 定义一个例外
    my_exception exception;  
    begin
      update emp set sal=sal+1000 where empno=emp_no;
      -- sql%notfound这是表示没有update
      -- raise my_exception;触发my_exception例外
      -- 什么时候触发
      if sql%notfound then
        raise my_exception;
      end if;
    -- 触发时做什么
    exception
      when my_exception then
        dbms_output.put_line('没有更新任何数据');
    end;

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值