oracle将例外分为预定义例外,非预定义例外和自定义例外三种。
预定义例外用于处理常见的oracle错误
非预定义例外用于处理预定义例外不能处理的例外
自定义例外用于处理与oracle错误无关的其它情况
declare
v_enamevarchar2(10);
v_salnumber(7,2);
begin
selectename,salintov_ename,v_salfromempwhereempno=&no;
dbms_output.put_line('Name is:'||v_ename||v_sal);
exception
whenno_data_foundthen
dbms_output.put_line('Cannot find the worker you wanted');
end;
!!!
处理预定义异常
预定义异常是由plsql所提供的系统异常。当plsql应用程序违反了oracle规定的限制时,则会隐含的触发一个内部异常。plsql为开发人员提供了20多个预定义异常。
!!!
预定义异常case_not_found
在开发pl/sql块中编写case语句时,如果在when子句中没有包含必须的条件分支,就会触发case_not_found的异常:
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
update emp set sal = sal + 100 where empno = spno;
when v_sal
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;
!!!预定义异常cursor_already_open
当重新打开已经打开的游标时,会隐含的触发异常cursor_already_opendeclare
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;
/
!!!
预定义异常dup_val_on_index
在唯一索引所对应的列上插入重复的值时,会隐含的触发异常dup_val_on_index例外
begin
insert into dept values (10, '公关部', '北京');
exception
when dup_val_on_index then
dbms_output.put_line('在deptno列上不能出现重复值');
end;
!!!
预定义异常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;
!!!预定义异常invalid_number
当输入的数据有误时,会触发该异常
比如:数字100写成了loo就会触发该异常
begin
update emp set sal= sal + 'loo';
exception
when invalid_number then
dbms_output.put_line('输入的数字不正确');
end;
!!!
预定义异常no_data_found
下面是一个pl/sql块,当执行select into 没有返回行,就会触发该异常
declare
v_sal emp.sal%type;
begin
select sal into v_sal from emp
when ename='&name';
exception
when no_data_found then
dbms_output.put_line('不存在该员工');
end;
!!!预定义异常too_many_rows
当执行select into语句时,如果返回超过了一行,则会触发该异常。
Sql代码
declare
v_ename emp.ename%type;
begin
select ename into v_ename from emp;
exception
when too_many_rows then
dbms_output.put_line('返回了多行');
end;
!!!预定义异常value_error
当在执行赋值操作时,如果变量的长度不足以容纳实际数据,则会触发该异常value_error,比如:
declare
v_ename varchar2(5);
begin
select ename into v_ename from emp where empno = &no1;
dbms_output.put_line(v_ename);
exception
when value_error then
dbms_output.put_line('变量尺寸不足');
end;
!!!
!!!
!!!
其它预定义异常(这些异常不是在pl/sql里触发的,而是在用oracle时触发的,所以取名叫其它预定义异常)
1.login_denied
当用户非法登录时,会触发该异常
2.not_logged_on
如果用户没有登录就执行dml操作,就会触发该异常
3.storage_error
如果超过了内存空间或是内存被损坏,就触发该异常
4.timeout_on_resource
如果oracle在等待资源时,出现了超时就触发该异常
预定义异常和自定义异常都是与oracle错误相关的,并且出现的oracle错误会隐含的触发相应的例外;而自定义异常与oracle错误没有任何关联,它是由开发人员为特定情况所定义的异常。
问题:请编写一个pl/sql块,接收一个雇员的编号,并给该雇员工资增加1000元,如果该雇员不存在,请提示。
--自定义例外
create or replace procedure ex_test(spNo number)
is
begin
--更新用户sal
update emp set sal=sal+1000 where empno=spNo;
end;
/
SQL> exec ex_test(56);
这里不报异常
修改代码,如下:
--自定义例外
create or replace procedure ex_test(spNo number)
is
--定义一个例外
myex exception;
begin
--更新用户sal
update emp set sal=sal+1000 where empno=spNo;
--sql%notfound这是表示没有update
--raise myex触发myex
if sql%notfound then
raise myex;
end if;
exception
when myex then
dbms_output.put_line('没有更新任何用户');
end;
/
declare
v_name my_emp.ename%type;
e exception;
begin
select ename into v_name from my_emp where sal=800;
if v_name <> 'xiaol' then
raise e;
end if;
dbms_output.put_line(v_name);
exception when e then
dbms_output.put_line('error:not expire');
end;