oracle中异常处理(被0除),oracle异常处理 - osc_uz0m1ri0的个人空间 - OSCHINA - 中文开源技术交流社区...

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

中文乱码解决:

--查看系统环境变量

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;

/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值