Question : Ref Cur

I have following Cursor which I want to return as Out parameter using a store Proc.
I want to return this refcursor only when ROWCOUNT > 0. Else return empty record set.
How can I write a PLSQL store procedure which return the refcursor?
When the records are getting processed we want to Lock using FOR UPDATE SKIP LOCKED.

Thanks
1:
2:
3:
4:
5:
SELECT ENO, ENAME, SAL FROM EMPLOYEE
WHERE SAL >= :V_LIMIT
AND DEPTNO = :V_DEPT
AND ROWNUM < 500
ORDER BY ENO;

Answer : Ref Cur

The quick answer is you really can't do that.

There isn't a way to check the rows returned in a cursor unless for fetch them. once you fetch them, you cannot reset the cursor back to the first row.

If the query runs relatively quick you can select the first row to see if there is at least one row.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
drop table tab1 purge;
create table tab1(col1 char(1));

insert into tab1 values('a');
insert into tab1 values('a');
insert into tab1 values('a');
commit;


create or replace procedure myProc(inChar in char, outCur out sys_refcursor)
is
	tmpChar char(1);
begin

	--check for data before the rest of the code
	begin
		select 'x' into tmpChar from tab1 where col1=inChar and rownum=1;
		exception
			when NO_DATA_FOUND then
				raise_application_error(-20002, 'No data found.  Cannot continue.');
	end;


	open outCur for select col1 from tab1 where col1=inChar;


end;
/

show errors


--test using SQL*Plus variables
var myCur refcursor

exec myProc('a',:myCur);

print myCur

exec myProc('b',:myCur);
Random Solutions  
 
programming4us programming4us