switch between two cursors based on parameter passed into stored procedure
I have two cursors in my procedure that only differ on the table name that they join to. The cursor that is used is determined by a parameter passed into the procedure
if (param = 'A') then
DECLARE CURSOR myCursor IS
SELECT x,y,z
FROM table1 a, table2 b
BEGIN
FOR aRecord in myCursor
LOOP
proc2(aRecord.x, aRecord.y, aRecord.z);
END LOOP;
COMMIT;
END;
elsif (param = 'B') then
DECLARE CURSOR myCursor IS
SELECT x,y,z
FROM table1 a, table3 b -- different tabl开发者_开发技巧e
BEGIN
FOR aRecord in myCursor
LOOP
proc2(aRecord.x, aRecord.y, aRecord.z);
END LOOP;
COMMIT;
END;
end if
I don't want to repeat the code for the sake of one different table. Any suggestions on how to improve this?
Thanks in advance
You can use a REF CURSOR like this (I used the EMP and DEPT tables for convenience):
declare
mycursor sys_refcursor;
param varchar2(1) := 'A';
type arecordtype is record (no integer, name varchar2(30));
arecord arecordtype;
begin
if (param = 'A') then
open mycursor for
select deptno as no, dname as name
from dept;
elsif (param = 'B') then
open mycursor for
select empno as no, ename as name
from emp;
else
raise_application_error(-20001,'Invalid param value: '||param);
end if;
loop
fetch mycursor into arecord;
exit when mycursor%notfound;
dbms_output.put_line(arecord.name);
end loop;
close mycursor;
end;
Though Tony Andrews proposed solution is the right way to do it, here is a little quick-and-dirty alternative way:
DECLARE
param varchar2(1) := 'A';
BEGIN
FOR aRecord IN ( SELECT x,y,z FROM table1 a, table2 b
WHERE a.foo = b.foo /* join condition */
AND param = 'A'
UNION ALL
SELECT x,y,z FROM table1 a, table3 b
WHERE a.foo = b.foo
AND param = 'B' ) LOOP
proc2(aRecord.x, aRecord.y, aRecord.z);
END LOOP;
COMMIT;
END;
Obviously, this can be considerably slower than the clean solution proposed by Tony.
精彩评论