是否可以在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
语句为真来填充游标。
答案 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;