sql%notfound与exception

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 。原因:

  1. 如果select into 没有返回值,那么会在sql%notfound之前调用exception
  2. 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.

猜你喜欢

转载自blog.csdn.net/qq_43179428/article/details/109258521