--exception异常
declare
v_sal employees.salary%type;
begin
select salary into v_sal from employees where employee_id=100;
exception
--非预定义异常
when too_many_rows then
dbms_output.put_line('不只一条数据');
when others then
dbms_output.put_line('出现其它异常');
end;
set serveroutput on;
--pragna的使用,将自定义异常与预定义异常相关联
declare
delete_exception exception;
pragma exception_init(delete_exception ,-2292);
begin
delete from employees where employee_id=100;
Exception WHEN delete_exception then
dbms_output.put_line('非完整性异常');
end;
--raise的使用,若工资高于6000抛出自定义异常
declare
v_sal employees.salary%type;
v_high_sal exception;
begin
select salary into v_sal from employees where employee_id=100;
if v_sal >6000 then raise v_high_sal;
end if;
exception
when v_high_sal THEN dbms_output.put_line('工资过高');
end;
set serveroutput on;
--抛出预定义异常,no_data_found,查询员工 ,若没有该员工,则抛出异常
declare
v_sal employees.salary %type;
begin
select salary into v_sal from employees where employee_id=1000;
exception
when no_data_found then dbms_output.put_line('未找到相应数据');
declare
v_sal employees.salary%type;
begin
select salary into v_sal from employees where employee_id=100;
exception
--非预定义异常
when too_many_rows then
dbms_output.put_line('不只一条数据');
when others then
dbms_output.put_line('出现其它异常');
end;
set serveroutput on;
--pragna的使用,将自定义异常与预定义异常相关联
declare
delete_exception exception;
pragma exception_init(delete_exception ,-2292);
begin
delete from employees where employee_id=100;
Exception WHEN delete_exception then
dbms_output.put_line('非完整性异常');
end;
--raise的使用,若工资高于6000抛出自定义异常
declare
v_sal employees.salary%type;
v_high_sal exception;
begin
select salary into v_sal from employees where employee_id=100;
if v_sal >6000 then raise v_high_sal;
end if;
exception
when v_high_sal THEN dbms_output.put_line('工资过高');
end;
set serveroutput on;
--抛出预定义异常,no_data_found,查询员工 ,若没有该员工,则抛出异常
declare
v_sal employees.salary %type;
begin
select salary into v_sal from employees where employee_id=1000;
exception
when no_data_found then dbms_output.put_line('未找到相应数据');
end;
--自定义异常,隐式游标
declare
no_result exception;
begin
update employees1 set salary=salary+100 where employee_id=1000;
if sql%notfound then
raise no_result;
end if;
exception
when no_result then
dbms_output.put_line('未找到该员工 ');
end;