create or replace procedure obj2_06(no number) as
job emp1.job%type;
unconsistent exception;
begin
select job into job from emp
where empno=no;
if sql%notfound then
dbms_output.put_line('11');
end if;
exception
when no_data_found then
dbms_output.put_line('输入的编号没有对应的雇员');
end;
当查不到时,11永远不会打印。
SQL%NOTFOUND 对于 PL/SQL SELECT INTO 。原因:
- 如果select into 没有返回值,那么会在sql%notfound之前调用exception
- SELECT INTO语句调用SQL聚合函数始终返回一个值(可能为NULL)。 在这样的语句之后,SQL%NOTFOUND属性始终为FALSE,因此无需检查。
A SELECT INTO statement that invokes a SQL aggregate function always returns a value (possibly NULL). After such a statement, the SQL%NOTFOUND attribute is always FALSE, so checking it is unnecessary.