Oracle:在不同的IF语句中打开相同的游标

时间:2014-10-08 16:55:47

标签: sql oracle

是否可以在IF语句块中打开游标,如下所示:

PROCEDURE CURSORIFSTATEMENT (param IN INT, iocursor IN OUT t_cursor)
IS
  v_cursor t_cursor;
BEGIN
  IF param = 1 THEN
    OPEN v_cursor FOR

    SELECT a.field
    FROM table1 a
    INNER JOIN table2 b ON b.fieldid = a fieldid;

    io_cursor := v_cursor;
    CLOSE v_cursor;
  ELSIF param = 2 THEN
    OPEN v_cursor FOR

    SELECT a.field, c.field
    FROM table1 a
    INNER JOIN table2 b ON b.fieldid = a.fieldid
    INNER JOIN table3 c ON c.fieldid = a.fieldid;

    io_cursor := v_cursor;
    CLOSE v_cursor;
  END IF;
END CURSORIFSTATEMENT;

基本上,目标是根据哪个IF语句为真来填充游标。

1 个答案:

答案 0 :(得分:1)

我认为您的要求和尝试没有任何问题。但是,由于游标中不允许IF-ELSE条件,因此,在PL/SQL中,您可以使用REF CURSOR

DECLARE 
  CUR REF CURSOR ;
    BEGIN 
       IF (so and so) THEN 
         OPEN CUR IS 'SELECT * FROM table_1'; 
       ELSE (so and so)
         OPEN CUR IS 'SELECT * FORM table_2';
     END IF ;
END;