switch between two cursors based on parameter passed into stored procedure

Posted by db83 on Stack Overflow See other posts from Stack Overflow or by db83
Published on 2010-03-25T10:35:21Z Indexed on 2010/03/25 10:43 UTC
Read the original article Hit count: 351

Hi,

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 table

    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

© Stack Overflow or respective owner

Related posts about plsql

Related posts about oracle10g