PL/SQL - 如何创建条件游标?
我需要有一个条件游标,例如:
- 如果存在一行(使用WHEN EXISTS或类似的东西),那么我的游标是:
- CURSOR varCursor IS SELECT 1 a FROM DUAL;
- 别的
- CURSOR varCursor IS SELECT 2 a FROM DUAL;
但是看,我不想更改列结果,我想更改整个光标。
下面我举一个更大的例子。
谢谢!
看:
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;
I need to have a conditional cursor, like:
- If a row exists (using WHEN EXISTS or something like this), then my cursor is:
- CURSOR varCursor IS SELECT 1 a FROM DUAL;
- Else
- CURSOR varCursor IS SELECT 2 a FROM DUAL;
But look, I don't want to change a column result, I want to change the entire cursor.
Bellow I put a bigger example.
Thanks!
See:
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非将其放入一个查询中(正如托尼建议的那样),因为您想要一个游标结果,您可以这样做(这会将游标切换到您需要的逻辑 - >一个游标解决方案)
barring putting it into one query (as Tony recommends) since you want one cursor result, you can do it as such (this will switch the cursor to the logic you need --> one cursor solution)
从字面上看,您可以这样做:
但是,拥有 2 个游标并打开合适的游标会更简单,也可能更有效。
Literally, you could do this:
However, it would be simpler and perhaps more efficient to have 2 cursors and open whichever is appropriate.