对PLSQL的SQL%NOTFOUND的再验证
开始
想要知道,是否在一条SQL执行完毕之后,有否机会在接下来的程序运行段看SQL%NOTFOUND的值。
事实上,它立即跳到了异常段。
SET SERVEROUTPUT ON; DECLARE v_ename emp.ename%TYPE; e_norecord EXCEPTION; PRAGMA EXCEPTION_INIT (e_norecord, -20001); err_num NUMBER; err_msg VARCHAR2(100); BEGIN SELECT ename INTO v_ename FROM emp where empno=3000; IF SQL%NOTFOUND THEN DBMS_OUTPUT.PUT_LINE('I can not found!'); RAISE_APPLICATION_ERROR(-20001,'No records found!'); END IF; EXCEPTION WHEN e_norecord THEN DBMS_OUTPUT.PUT_LINE('Yes! No records found!!'); WHEN OTHERS THEN err_num := SQLCODE; err_msg := SUBSTR(SQLERRM, 1, 100); DBMS_OUTPUT.PUT_LINE(err_msg||err_num); END;
运行的结果:
anonymous block completed ORA-01403: no data found100
结束