我需要一个条件光标,例如:
但是看,我不想改变列结果,我想改变整个游标。
贝娄我举了一个更大的例子。
谢谢!
请参阅:
SET serveroutput ON SIZE 900000;
DECLARE
CURSOR varCursor IS SELECT 1 a FROM DUAL;
-- CURSOR varCursor IS SELECT 2 a FROM DUAL;
BEGIN
FOR varRow IN varCursor LOOP
dbms_output.put_line('row: ' || varRow.a);
END LOOP;
dbms_output.put_line('Done.');
END;
答案 0 :(得分:11)
禁止将其放入一个查询中(如Tony建议的那样),因为您需要一个游标结果,您可以这样做(这会将光标切换到您需要的逻辑 - >一个游标解决方案)
DECLARE
PROCEDURE CURSORCHOICE(ITEM IN NUMBER) IS
L_REFCUR SYS_REFCURSOR;
returnNum number;
BEGIN
IF NVL(ITEM,0) > 0 THEN
OPEN L_REFCUR FOR
SELECT ITEM * level FROM DUAL
CONNECT BY LEVEL < ITEM ;
ELSE
OPEN L_REFCUR FOR
SELECT ITEM - LEVEL FROM DUAL
connect by level < -1 * ITEM ;
END IF;
dbms_output.put_line('Results to item ' || item);
loop
fetch l_refcur into returnNum;
exit when l_refcur%notfound;
dbms_output.put_line(returnNum);
end loop;
CLOSE L_REFCUR;
END ;
BEGIN
CURSORCHOICE(5);
CURSORCHOICE(-5);
end ;
/
Results to item 5
5
10
15
20
Results to item -5
-6
-7
-8
-9
答案 1 :(得分:7)
从字面上看,你可以这样做:
CURSOR varCursor IS SELECT 1 a FROM DUAL WHERE EXISTS (...)
UNION
SELECT 2 a FROM DUAL WHERE NOT EXISTS (...);
但是,使用2个游标并以适当的方式打开它会更简单,也许更有效。