根据传入存储过程的参数在两个游标之间切换

时间:2010-03-25 10:35:22

标签: stored-procedures plsql oracle10g

我的程序中有两个游标,它们只与加入的表名不同。 使用的游标由传递给过程

的参数确定
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 table

    BEGIN
       FOR  aRecord in myCursor
       LOOP
          proc2(aRecord.x, aRecord.y, aRecord.z);       
       END LOOP;
       COMMIT;
    END;
end if

我不想为了一个不同的表重复代码。 关于如何改进这个的任何建议?

提前致谢

2 个答案:

答案 0 :(得分:3)

您可以像这样使用REF CURSOR(为方便起见,我使用了EMP和DEPT表):

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;

答案 1 :(得分:1)

虽然Tony Andrews建议解决方案是正确的方式,但这里有一种快速而又肮脏的替代方式:

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;

显然,这可能比Tony提出的清洁解决方案慢得多。