Originally posted by amolik
The Open cursor does not still work in Excpetion
Sure it does.
The only thing you must to do is to raise that exception:
Code:
scott@oracle> var c refcursor
scott@oracle> create or replace function Listemp(eid in number)
return pack1.cursortype is
l_cursor pack1.cursortype;
i number;
begin
-- this DEFINITELY raises NO_DATA_FOUND
SELECT 1 INTO i FROM dual WHERE 1=0;
exception when NO_DATA_FOUND then
open l_cursor for
select distinct 1 empno,'Smith' ename, 'Manager' job
from dual;
return l_cursor;
end;
/
Function created.
scott@oracle> exec :c:=Listemp(0);
PL/SQL procedure successfully completed.
scott@oracle> print c
EMPNO ENAME JOB
---------- ----- -------
1 Smith Manager
I'd suggest this adjustment of your function:
Code:
scott@oracle> var c refcursor
scott@oracle> create or replace function Listemp(eid in number)
return pack1.cursortype is
l_cursor pack1.cursortype;
l_eid number;
begin
-- this checks for eid and eventually raises NO_DATA_FOUND
SELECT empno INTO l_eid FROM emp WHERE empno=eid;
open l_cursor for
select empno, ename, job
from emp
where empno = eid;
return l_cursor;
exception when NO_DATA_FOUND then
open l_cursor for
select distinct 1 empno,'Smith' ename, 'Manager' job
from dual;
return l_cursor;
end;
/
Function created.
scott@oracle> exec :c:=Listemp(0);
PL/SQL procedure successfully completed.
scott@oracle> print c
EMPNO ENAME JOB
---------- ----- -------
1 Smith Manager
scott@oracle> exec :c:=Listemp(7499)
PL/SQL procedure successfully completed.
scott@oracle> print c
EMPNO ENAME JOB
---------- ---------- ---------
7499 ALLEN SALESMAN
HTH,
Ales
Bookmarks