CREATE OR REPLACE function get_employees (pID in varchar2) return Number
As
salary Number;
begin
begin
select e.salary into salary from employees e where employee_id=pID;
EXCEPTION WHEN NO_DATA_FOUND THEN
salary := -1;
END;
return salary;
end get_employees;
CREATE OR REPLACE function get_employees (id in varchar2) return varchar2 is
sal employees.salary%type;
begin
select salary into sal from employees where employee_id=id;
return sal;
EXCEPTION WHEN NO_DATA_FOUND THEN
raise_application_error(-20001, 'Custom NO_DATA_FOUND');
end get_employees;
SET SERVEROUTPUT ON ;
/
CREATE OR REPLACE FUNCTION FN_CHK
RETURN NUMBER
IS
V_CHK NUMBER :=1;
BEGIN
FOR I IN 1..100
LOOP
DBMS_OUTPUT.PUT_LINE(I);
IF(I>=4) THEN
--DBMS_OUTPUT.PUT_LINE('BREAK');
V_CHK:=0;
--V_CHK:=0/0;
exit;
END IF;
END LOOP;
RETURN V_CHK;
EXCEPTION WHEN OTHERS THEN
V_CHK:=1000;
RETURN V_CHK;
END;
/
SELECT FN_CHK
FROM DUAL;
3条答案
按热度按时间fumotvh31#
如果抛出异常,则没有返回值。引发异常,调用堆栈在此结束。
当然,您可以在函数中使用no_data_found:
5q4ezhmt2#
不能在函数中使用NO_DATA_FOUND,因为它将给予一个预定义的异常,用于空返回。
igetnqfo3#
希望下面的例子有助于解决退货问题......