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

    (&变量名称:运行时输入的变量)

    中文乱码解决:

    --查看系统环境变量

    select * from nls_database_parameters;

    NLS_LANGUAGE.NLS_TERRITORY.NLS_CHARACTERSET

    编辑配置文件:

    export LANG=zh_CN.utf8

    export NLS_LANG=AMERICAN.AMERICA.WEBMSWIN1252

    export SQLPATH=/home/oracle

    export EDITOR=vi

    export sqlplus='rlwrap sqlplus'

    使修改后的配置文件立即生效:. !$

    处理预定义异常:

    declare

      t_name varchar2(50);

    begin

      select user_name into t_name from t_user where user_code=$t_usercode;

      dbms_output.put_line(t_name);

    exception

      when no_data_found then

        dbms_output.put_line('未查询到数据!');

      when too_many_rows then

        dbms_output.put_line('向标量填充太多元素!');

    end;

    捕获oracle错误(定义异常类型的变量,与oracle错误代码关联)

    declare

      own_error exception;

      pragma exception_init(own_error, -2291); 

    begin

      update t_user set user_name=&t_name where user_code=&t_usercode;

    exception

      when own_error then

        dbms_output.put_line('主键不存在!');

    end;

    /

    使用others捕获所有没有考虑到的异常:

    declare

      t_flag char(1);

      t_usercode number:=&m_usercode;

    begin

      select 'C' indo t_flag from t_user where user_code=t_usercode;

    exception

      when others then

        dbms_output.put_line(sqlcode||'--'||sqlerrm);--sqlcode表示异常代码,sqlerrm表示异常信息

    end;

    /

    (除了100--ORA-01403: no data found,所有的错误代码都是ORA后面跟的数字)

    自定义异常:ORA-20000 ~ ORA-20999(oracle提供的自定义异常代码取值范围)

    declare

    begin

      raise_application_error(-20000, '不能修改人员代码‘);

      update t_user set user_code=1 where user_name='老大';

    end;

    /

    declare

      own_error exception;

      pragma exception_init(own_error, -20000);

    begin

      if to_char(sysdate, 'DY') in ('SAT', 'SUN') then

        raise_application_error(-20000, '周末必须休息!');

      else

        update t_user set apple=apple+3;

      end if;

    exception

      when own_error then

        dbms_output.put_line(sqlcode||'--'||sqlerrm);

    end;

    /

  • 相关阅读:
    IEqualityComparer<T> 重写注意事项
    InfoPath使用Sharepoint Webservice之多参数
    强制使用office web Apps新建文档
    SQL 分组取每组第N行数据
    Sharepoint Ribbon 开启右键菜单(此文作废)
    sharepoint 2010 Infopath 备忘
    sharepoint windows认证模式下 限制人员选取器能访问OU
    Unable to load configuration异常处理
    Java数据库连接池的配置
    No Suitable Driver Found 解决方法
  • 原文地址:https://www.cnblogs.com/cyf18/p/10693446.html
Copyright © 2011-2022 走看看