涉及到表的处理请参看原表结构与数据  Oracle建表插数据等等

使用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系列之异常处理的更多相关文章

  1. .Net程序员学用Oracle系列(25):触发器详解

    1.触发器理论 1.1.触发器的应用场景 1.2.触发器的类型 1.3.DML 触发器的触发顺序 2.触发器实战 2.1.创建触发器 2.1.1.创建 DML 触发器 2.1.2.创建 DDL 触发器 ...

  2. C#进阶系列——WebApi 异常处理解决方案

    前言:上篇C#进阶系列——WebApi接口传参不再困惑:传参详解介绍了WebApi参数的传递,这篇来看看WebApi里面异常的处理.关于异常处理,作为程序员的我们肯定不陌生,记得在介绍 AOP 的时候 ...

  3. Oracle系列:记录Record

    Oracle系列:记录Record   分类: [Oracle] (15) 版权声明:本文为博主原创文章,未经博主允许不得转载. Oracle系列:记录(Record) 一,什么是记录(Record) ...

  4. 足球和oracle系列(3):oracle过程排名,世界杯第二回合战罢到来!

    足球与oracle系列(3):oracle进程排名.世界杯次回合即将战罢! 声明:        这不是技术文档,既然学来几招oracle简单招式.就忍不了在人前卖弄几下.纯为茶余饭后与数朋库友的插科 ...

  5. .Net程序员学用Oracle系列(1):导航目录

    本人从事基于 Oracle 的 .Net 企业级开发近三年,在此之前学习和使用的都是 (MS)SQL Server.未曾系统的了解过 Oracle,所以长时间感到各种不习惯.不方便.怪异和不解,常会遇 ...

  6. .Net程序员学用Oracle系列(2):准备测试环境

    <.Net程序员学用Oracle系列:导航目录> 本文大纲 1.创建说明 1.1.为什么要创建的测试环境? 1.2.了解 Oracle 实例的默认用户 2.创建环境 2.1.创建基本环境 ...

  7. .Net程序员学用Oracle系列(6):表、字段、注释、约束、索引

    <.Net程序员学用Oracle系列:导航目录> 本文大纲 1.表 1.1.创建表 1.2.修改表 & 删除表 2.字段 2.1.添加字段 2.2.修改字段 & 删除字段 ...

  8. .Net程序员学用Oracle系列(7):视图、函数、过程、包

    <.Net程序员学用Oracle系列:导航目录> 本文大纲 1.视图 1.1.创建视图 2.函数 2.1.创建函数 2.2.调用函数 3.过程 3.1.创建过程 3.2.调用过程 4.包 ...

  9. .Net程序员学用Oracle系列(8):触发器、任务、序列、连接

    <.Net程序员学用Oracle系列:导航目录> 本文大纲 1.触发器 1.1.创建触发器 1.2.禁用触发器 & 启用触发器 & 删除触发器 2.任务 2.1.DBMS_ ...

随机推荐

  1. MarkDown中锚点的使用

    在文档中创建锚点: <A NAME="ROP_ON_ARM">Davi L, Dmitrienko A, Sadeghi A R, et al. [Return-ori ...

  2. Windows 键盘操作快捷方式积累

    复制.粘贴: CTRL+C 复制被选择的项目到剪贴板 CTRL+V 粘贴剪贴板中的内容到当前位置 CTRL+X 剪切被选择的项目到剪贴板 Alt+ space + E + P CMD 窗口快速粘贴 关 ...

  3. 伪分布式环境下命令行正确运行hadoop示例wordcount

    首先确保hadoop已经正确安装.配置以及运行. 1.     首先将wordcount源代码从hadoop目录中拷贝出来. [root@cluster2 logs]# cp /usr/local/h ...

  4. Jquery-Mobile滚动事件

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> < ...

  5. npm:Node.js的软件包管理器

    npm https://www.npmjs.com/ 2016-08-03

  6. 相册弹窗(基于zepto.js)

    //放大图片 $(page).on('click','.popupImage img',function () { var that = $(this); that.popupImage({ this ...

  7. jQuery.hhLRSlider 左右滚动图片插件

    /**  * jQuery.hhLRSlider 左右滚动图片插件  * User: huanhuan  * QQ: 651471385  * Email: th.wanghuan@gmail.com ...

  8. 关于Active控件的电子签名 转

    关于Active控件的电子签名 两种方案:一是自己制作证书,客户端安装证书后就可以识别该控件:二就是买官方的喽,在国内找verisign的代理,负责各种电子签名,任何一台浏览器都可以识别该证书.该公司 ...

  9. AngularJS(4)-服务(Service)

    1.$location服务 $location 服务,它可以返回当前页面的 URL 地址 2.$http服务 $http 是 AngularJS 应用中最常用的服务. 服务向服务器发送请求,应用响应服 ...

  10. RepeatedDNASequences BestTime_to_Buy_and_SellStockIV

    /** * @Author: weblee * @Email: likaiweb@163.com * @Blog: http://www.cnblogs.com/lkzf/ * @Time: 2015 ...