首页 / ORACLE / Oracle系列之异常处理
Oracle系列之异常处理
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Oracle系列之异常处理,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2983字,纯文字阅读大概需要5分钟。
内容图文
![Oracle系列之异常处理](/upload/InfoBanner/zyjiaocheng/460/4b8990ecb7bb4900ae0c782a57d06c2c.jpg)
使用select into语句读取tb_Employee的一行,使用异常处理处理no_data_found和two_many_rows的系统预定义异常
set serveroutput on; declare emp tb_Employee%rowtype; begin select * into emp from tb_Employee where ename = ‘SCOTT‘; dbms_output.put_line(‘SCOTT ‘‘s sal is : ‘ || emp.sal); exception when no_data_found then dbms_output.put_line(‘no data found exception‘); end; /
declare emp tb_Employee%rowtype; begin select * into emp from tb_Employee where deptno =20; dbms_output.put_line(‘The sal is : ‘ || emp.sal); exception when too_many_rows then dbms_output.put_line(‘too many rows exception‘); end; /
使用嵌套异常端处理,循环读取emp_id_minval 和 emp_id_maxval之间的员工,使得其中存在不存在员工号。输出对应员工的ename,不存在的员工输出“not exists such empolyee”。
declare emp_id tb_Employee.pk_Employee_ID%type; v_ename tb_Employee.ename%type; emp_id_minval tb_Employee.pk_Employee_ID%type; emp_id_maxval tb_Employee.pk_Employee_ID%type; begin emp_id_minval:=&emp_id_minval; emp_id_maxval:=&emp_id_maxval; for emp_id in emp_id_minval..emp_id_maxval loop begin select ename into v_ename from tb_Employee where pk_Employee_ID=emp_id; dbms_output.put_line(‘ename:‘||v_ename); exception when no_data_found then dbms_output.put_line(‘not exists such empolyee‘); end; end loop; end; /
写一个处理ora-01400 (不能插入空值)的系统异常的示例程序和异常处理
declare insert_excep exception; pragma exception_init(insert_excep, -01400); begin insert into tb_Department (pk_Department_ID, dname) values (50, null); exception when insert_excep then dbms_output.put_line(‘INSERT OPERATION FAILED‘); dbms_output.put_line(SQLERRM); end; /
使用SQLCODE,SQLERRM,获得异常的编号和错误信息,并输出异常的编号和错误信息
declare emp tb_Employee % rowtype; error_code number; error_message varchar2(255); begin select * into emp from tb_Employee where ename = ‘SCOTT‘; dbms_output.put_line(‘SCOTT ‘‘s salary is : ‘ || emp.sal); exception when too_many_rows then error_code := SQLCODE; error_message := SQLERRM; dbms_output.put_line(error_code || ‘ ‘ || error_message); end; /
自定义一个异常,抛出这个异常并处理
declare invalid_department exception; name varchar2(20) := ‘&name‘; deptno number := &deptno; begin update tb_Department set dname = name where pk_Department_ID = deptno; if sql % notfound then raise invalid_department; end if; exception when invalid_department then dbms_output.put_line(‘No such department‘); end; /
使用raise_application_error抛出一个应用异常
declare v_ename tb_Employee.ename%type:=‘&v_ename‘; e_name exception; pragma exception_init(e_name, -20999); begin delete from tb_Employee where ename = v_ename; if sql % notfound then raise_application_error(-20999, ‘This is not a valid ename‘); end if; exception when e_name then dbms_output.put_line(‘The ename ‘||v_ename||‘ exists, Please choose again‘); end; /
Oracle系列之异常处理
标签:
本文系统来源:http://www.cnblogs.com/tufujie/p/5075956.html
内容总结
以上是互联网集市为您收集整理的Oracle系列之异常处理全部内容,希望文章能够帮你解决Oracle系列之异常处理所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。