zoukankan      html  css  js  c++  java
  • plsql--异常处理

    一:基本介绍:plsql中的异常处理

    异常(EXCEPTION)类型:

    1.预定义(Predefined)错误  常用的:没有结果:No_data_found  输出的行太多:Too_many_rows

    2.非预定义(UnPredefined)错误

    3.用户定义(User_define)错误

    异常的格式:

    exception

      when Too_many_rows then dbms_output.put_line('输出的行数太多了');

    二:错误处理

    1.预定义异常

    [预定义异常]
    declare
    
      v_sal employees.salary%type;
    begin
      select salary into v_sal
      from employees
      where employee_id >100;
      
      dbms_output.put_line(v_sal);
    
    exception
      when Too_many_rows then dbms_output.put_line('输出的行数太多了');
    end;

    2.非预定义异常

      (1)声明异常 delete_mgr_excep exception;

      (2)把自定义的错误和oracle的错误编号关联起来,将异常的编号自动转换为定义的异常

        PRAGMA EXCEPTION_INIT(delete_mgr_excep,-2292);

      (3)处理异常

        when delete_mgr_excep then dbms_output.put_line('Manager不能直接被删除');

    [非预定义异常]
    declare
    
      v_sal employees.salary%type;
      --声明一个异常
      delete_mgr_excep exception;
      --把自定义的异常和oracle的错误关联起来
      PRAGMA EXCEPTION_INIT(delete_mgr_excep,-2292);
    begin
      delete from employees
      where employee_id = 100;
      
      select salary into v_sal
      from employees
      where employee_id >100;
      
      dbms_output.put_line(v_sal);
    
    exception
      when Too_many_rows then dbms_output.put_line('输出的行数太多了');
      when delete_mgr_excep then dbms_output.put_line('Manager不能直接被删除');
    end;

    3.用户定义异常

      (1)声明一个异常 too_high_sal exception;

      (2)抛出一个异常  raise too_high_sal;

      (3)处理异常 when too_high_sal then dbms_output.put_line('工资过高了');

    [用户自定义异常]
    declare
    
      v_sal employees.salary%type;
      --声明一个异常
      delete_mgr_excep exception;
      --把自定义的异常和oracle的错误关联起来
      PRAGMA EXCEPTION_INIT(delete_mgr_excep,-2292);
      
      --(1)声明一个异常
      too_high_sal exception;
    begin
    
      select salary into v_sal
      from employees
      where employee_id =100;
      
      if v_sal > 1000 then
        --(2)抛出一个异常
    raise too_high_sal; end if; delete from employees where employee_id = 100; dbms_output.put_line(v_sal); exception when Too_many_rows then dbms_output.put_line('输出的行数太多了'); when delete_mgr_excep then dbms_output.put_line('Manager不能直接被删除'); --(3)处理异常 when too_high_sal then dbms_output.put_line('工资过高了'); end;
  • 相关阅读:
    Qt画笔实现折线图
    Qt动态布局
    ffmpeg录制流媒体,正常方式停止录制
    解决libvlc_media_player_stop时死锁的方法
    Ubuntu 16 修改时区!
    qt窗口最小化之后无法打开
    Qt 之 去除窗口部件被选中后的焦点虚线框
    WINDOWS中, 如何查看一个运行中的程序是64位还是32位的
    DHTMLX学习总结
    mui plus.uploader.createUpload 上传文件服务端获取文件名中文乱码问题
  • 原文地址:https://www.cnblogs.com/cbpm-wuhq/p/11958301.html
Copyright © 2011-2022 走看看