oracle异常

oracle_exception.sql


EXCEPTION
when exception1 [or exception2...] then
statement;...

[when exception1 [or exception2...] then
statement;...]

[when others then
statement1;...]
sqlplus scott/tiger

1.常见的预定义异常
(1)no_data_found
select语句异常用no_data_found
根据雇员编号获取雇员名称
set  serveroutput on;
declare
v_ename emp.ename%type;
begin
select ename into v_ename from emp where empno=&no;
dbms_output.put_line('ename '||v_ename);

exception
when no_data_found then
dbms_output.put_line('empno is not exists');
end;


(2)dup_val_on_index
更新dept表中的数据,导致主键重复

set serveroutput on;
declare
begin
update dept set deptno=&deptno where upper(dname)=upper('&dname');
exception
when dup_val_on_index then
dbms_output.put_line('deptno exists');
end;

begin
update dept set deptno=&newno where deptno=&oldno;
exception
when dup_val_on_index then
dbms_output.put_line('primary key duplicat.');
end;
(3)too_many_rows
获取员工名的时候,使用查询条件salary
set serveroutput on;
declare
v_ename emp.ename%type;
begin
select ename into v_ename from emp where sal=&salary;
dbms_output.put_line(v_ename);
exception
when too_many_rows then
dbms_output.put_line('ename is too many rows');
end;

2.异常函数sqlcode,sqlerrm
create table errors(error_id number,program_name varchar2(50),error_code number,error_message varchar2(100),create_date date default sysdate,constraint pk_error primary key(error_id));

create sequence seq_errors
minvalue 1
maxvalue 999999999999999999
start with 1
increment by 1
cache 20;

set serveroutput on;
declare
v_ename emp.ename%type;
v_error_code number;
v_error_message varchar2(100);
begin
select ename into v_ename from emp where sal=&v_sal;
exception
when others then
v_error_code := sqlcode;
v_error_message := substr(sqlerrm,1,100);
insert into errors(error_id,program_name,error_code ,error_message)
values(seq_errors.nextval,'anonymous block',v_error_code,v_error_message);
commit;
end;


3.raise_application_error
语法:
raise_application_error(-Oracle_error_number,error_message)
-Oracle_error_number:-20000 到-20999
DECLARE
   num_tables NUMBER;
BEGIN
   SELECT COUNT(*) INTO num_tables FROM USER_TABLES;
   IF num_tables < 1000 THEN
      raise_application_error(-20101, 'Expecting at least 1000 tables');
   ELSE
      NULL;
   END IF;
END;

4.自定义异常

定义异常--->显示触发异常--->捕获处理异常
触发异常:为了处理与Oracle错误无关的异常情况,需要在执行部分出现异常情况时使用RAISE语句显式地触发异常。
DECLARE
   out_of_stock     EXCEPTION;
   number_on_hand   NUMBER (4);
BEGIN
   ...
  IF number_on_hand < 1 THEN
    RAISE out_of_stock;
  END IF;
EXCEPTION
  WHEN out_of_stock THEN
    -- handle the error
END;


PRAGMA EXCEPTION_INIT(exception_name, -Oracle_error_number);
将程序中所声明的异常标示符和oracle一个特定的错误号关联起来。
Oracle_error_number:oracle 特定的错误编码ORA-error_number,oracle会自动抛出,不用显式抛出
例如:
-2291 :外键不存在

raise_application_error:
。平时用来测试的异常处理我们都是通过dbms_output.put_line来输出异常信息,但是在实际的应用中,需要把异常信息返回给调用的客户端。其实 RAISE_APPLICATION_ERROR 是将应用程序专有的错误从服务器端转达到客户端应用程序(其他机器上的SQLPLUS或者其他前台开发语言)

set serveroutput on;
declare
e_no_deptno exception;
PRAGMA EXCEPTION_INIT(e_no_deptno,-2291);
e_no_empno exception;
begin
update emp set deptno = &v_deptno where empno=&v_empno;
if sql%notfound then
raise e_no_empno;
end if;
exception
when e_no_empno then
dbms_output.put_line('empno is not exist.');
when e_no_deptno then
dbms_output.put_line('deptno is not exist.');
end;




转载于:https://my.oschina.net/u/1458120/blog/225917

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值