The main body of Oracle functions is as follows:
Create or replace function fn_get_agmt_bal (p_agmt_no varchar2)
RETURN NUMBER IS
V_bal NUMBER: = 0;
-- If dbms_output takes effect during SQL/PLUS execution, run [SET SERVEROUTPUT ON] first ];
BEGIN
Select agmt_bal
Into v_bal
From edw_t01_agmt_bal_h
Where agmt_no = P_agmt_no;
RETURN v_bal;
EXCEPTION
WHEN NO_DATA_FOUND THEN
Dbms_output.put_line ('No data found. Please enter a valid protocol number! ');
Return null;
WHEN TOO_MANY_ROWS THEN
Dbms_output.put_line ('too much data is returned. Please enter a valid protocol number! ');
Return null;
WHEN OTHERS THEN
Dbms_output.put_line ('exception! ');
Return null;
END fn_get_agmt_bal;
/
Note:
1. The dbms_output package is used in the function. If dbms_output takes effect when executed in SQL/PLUS, run SET SERVEROUTPUT ON first ].
2. This is a common format for handling oracle exceptions, which can be applied in other cases.