博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
oracle异常处理
阅读量:4543 次
发布时间:2019-06-08

本文共 1639 字,大约阅读时间需要 5 分钟。

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

中文乱码解决:

--查看系统环境变量

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;

/

 

转载于:https://www.cnblogs.com/cyf18/p/10693446.html

你可能感兴趣的文章
jquery实现当前页面编辑
查看>>
初识rt-thread
查看>>
微服务架构下介质管理规范
查看>>
关于AutoCAD 2014的securityload…
查看>>
BM和KMP字符串匹配算法学习
查看>>
常用基本命令四(用户管理命令) - 黑猴子
查看>>
项目管理知识1
查看>>
在window环境下安装Python中的pip
查看>>
A大龙插件官方群3:621816328
查看>>
oi再见,你好明天。
查看>>
2018 Multi-University Training Contest 1 - D Distinct Values (STL+双指针)
查看>>
js学习笔记一-语法结构
查看>>
键盘对应的键值
查看>>
goLang 纳秒转 毫秒 转 英文时间格式
查看>>
微信小程序的坑坑
查看>>
图片轮播(Jquery)
查看>>
hdu 1704 Rank(floyd传递闭包)
查看>>
Educational Codeforces Round 27 G. Shortest Path Problem?(Guass异或线性基)
查看>>
【BZOJ3622】已经没有什么好害怕的了(动态规划+广义容斥)
查看>>
HDOJ 1023 Train Problem II
查看>>