先创建表:

CREATE TABLE TB_USER
(
    ID INTEGER PRIMARY KEY,
    USER_NAME VARCHAR2(20) NOT NULL,
    USER_AGE INTEGER NOT NULL
);

CREATE SEQUENCE SEQ_USER
INCREMENT BY 1
START WITH 1
NOMAXVALUE
NOCYCLE
CACHE 10;

CREATE OR REPLACE TRIGGER TR_USER BEFORE INSERT ON TB_USER FOR EACH ROW
BEGIN
    SELECT SEQ_USER.NEXTVAL INTO :NEW.ID FROM DUAL;
END;



插入一些数据。


创建NO_DATA_FOUND的存储过程:

CREATE OR REPLACE PROCEDURE PRO_NO_DATA_FOUND(USER_ID IN INTEGER) AS
V_AGE TB_USER.USER_AGE%TYPE;
V_NAME TB_USER.USER_NAME%TYPE;
BEGIN
  SELECT USER_NAME, USER_AGE INTO V_NAME, V_AGE FROM TB_USER WHERE ID=USER_ID;
  EXCEPTION
    WHEN NO_DATA_FOUND THEN
      DBMS_OUTPUT.put_line('no data found');
    WHEN OTHERS THEN
      DBMS_OUTPUT.put_line('ERROR CODE: ' || SQLCODE || CHR(10) || 'ERROR MSG:' || SQLERRM); 
END;



会显示NO_DATA_FOUND的记录如下:

SQL> SELECT * FROM TB_USER WHERE ID=1;

未选定行

SQL>



执行:

CALL PRO_NO_DATA_FOUND(1);



输出:

no data found