oracle----例外

oracle中的例外就是exception,先看段代码示例

declare 
v_ename emp.ename%type;
begin
select ename into v_ename from emp where empno=&no;
dbms_output.put_line('名字:'||v_ename);
exception
when no_data_found then
dbms_output.put_line('编号没有');


end;
其中,no_data_found是预定义例外


一共有20多个预定义意外,介绍下常用的几个
1.case_not_found
如果在when子句中没有包含必须的条件分支,就会触发。
create or replace procedure sp_pro6(spno number) is
v_sal emp.sal%type;
begin
select sal into v_sal from emp where empno=spno;
case
when v_sal<1000 then
update emp set sal=sal+100 where empno=spno;
when v_sal<2000 then
update emp set sal=sal+200 where empno=spno;
end case;
exception
when case_not_found then
dbms_output.put_line('case语句没有与'||v_sal||'相匹配的条件');
end;
以上的case,如果sal薪水大于2000,则没有对应的程序入口,会抛出意外


2.cursor_already_open
declare
cursor emp_cursor is select ename ,sal from emp;
begin
open emp_cursor; --打开游标
for emp_record1 in emp_cursor loop  -- 这里又一次打开游标
dbms_output.put_line(emp.record1.ename);
end loop;
exception
when cursor_already_open then
dbms_output.put_line('游标已经打开');
end;
游标打开后,被重复打开,会抛出这个异常


3.dup_val_on_index
在唯一索引所对应的列上插入重复的值时,会隐含的触发例外
begin
insert into dept values(10,'公关部','北京')
exception
when dup_val_on_index then
dbms_output.put_line('在deptno列上不能出现重复值');
end;


4.invalid_cursor
当试图在不合法的游标上操作时,会触发该例外
例如:试图从一个没有打开的游标提取数据,或是关闭没有打开的游标,会触发例外
declare
cursor emp_cursor is select ename ,sal from emp;
emp_record emp_cursor%rowtype;
begin
--open emp cursor;--打开游标
fetch emp_cursor into emp_record;
dbms_output.put_line(emp_record.ename);
close emp_cursor;
exception
when invalid_cursor then
dbms_output.put_line('请检测游标是否打开');
end;


5.invalid_number
当输入的数据有误是,触发该意外
比如数字100,写成了loo就会触发
begin
update emp set sal = sal+'loo';
exception
when invalid_number then
dbms_output.put_line('输入的数字不正确');
end;


6.no_data_found
当执行select into 没有返回行,触发该意外;
declare 
v_ename emp.ename%type;
begin
select ename into v_ename from emp where empno=&no;
dbms_output.put_line('名字:'||v_ename);
exception
when no_data_found then
dbms_output.put_line('编号没有');


end;


7.too_many_rows
当执行select into 语句时,如果返回的结果超过了一行,则会触发该例外
declare
v_ename emp.ename%type;
begin
select ename into v_ename from emp;
exception
when too_many_rows then
dbms_out.put_line('返回了多行');
end;


8.zero_divide
当执行 2/0操作语句时,分母是0时,触发该例外


9.value_error
当执行赋值操作时,如果变量的长度不足以容纳实际数据,则触发该例外
declare
v_ename varchar2(1);
begin
select ename into v_ename from emp where empno=&no;
dbms_out.put_line(v_ename);
exception
when value_error then
dbms_output.put_line('定义的变量容量不足');
end;


其他的预定义例外
login_denide:用户非法登录,触发
not_logged_on:用户没有登录就执行dml操作,触发
storage_error:超过了内存空间或是内存被破坏,触发
timeout_on_resource:如果oracle在等待资源时,出现了超时就触发。


自定义例外
自定义例外与oracle错误没有任何关联,由开发人员为特定情况所定义的例外
create or replace procedure ex_test(spNo number) is
--定义一个例外
myex exception;
begin
--更新用户sal
update emp set sal=sal+1000 where empno=spNo;
dbms_output.put_line('更新ok');
--sql%notfound,这是表示没有update
--raise myex 触发myex例外
if sql%notfound then
raise myex;
end if;
exception
when myex then
dbms_output.put_line('没有更新任何用户');
end;


exec ex_test(56);
--以前讲过一个no_data_found,这个地方不适用,no_data_found在select搜索不存在的时候会触发。
--如果进行update操作,没有数据对应,则不会触发。如果开发认为这个是错,这个时候可以自定义例外处理

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值