有条件地定义一个 Cursor
我在 Oracle 中有一个过程,它采用 varchar2 参数。根据该参数的值,我需要定义一个游标。游标会根据参数的值对不同的表进行操作。
我想做类似下面的事情,但它在 CURSOR
定义代码段中引发错误。有什么想法吗?
PROCEDURE GET_RECORDS(v_action IN VARCHAR2)
IS
CURSOR get_records
IS
IF(v_action = 'DO THIS') THEN
SELECT * from <THIS>;
ELSE
SELECT * from <THAT>;
END IF;
BEGIN
OPEN get_records;
FETCH get_records
INTO v_thing;
v_loop := 0;
WHILE get_records%FOUND
LOOP
FETCH get_records
INTO v_thing;
END LOOP;
CLOSE get_records;
END;
I have a Procedure in Oracle that takes a varchar2
paramater. Based on the value of that parameter, I need to define a cursor. The cursor will operate on different tables based on the value of the parameter.
I wanted to do something like below but it throws an error in the CURSOR
definition piece of code. Any ideas?
PROCEDURE GET_RECORDS(v_action IN VARCHAR2)
IS
CURSOR get_records
IS
IF(v_action = 'DO THIS') THEN
SELECT * from <THIS>;
ELSE
SELECT * from <THAT>;
END IF;
BEGIN
OPEN get_records;
FETCH get_records
INTO v_thing;
v_loop := 0;
WHILE get_records%FOUND
LOOP
FETCH get_records
INTO v_thing;
END LOOP;
CLOSE get_records;
END;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将需要一个 REF CURSOR 并有条件地打开它,例如:
you will need a REF CURSOR and open it conditionaly, for example:
我可能会编写类似这样的代码(其中两个循环可能调用相同的函数)
您也可以使用动态 SQL 来打开游标,但这往往会变得更加复杂,特别是如果只有两个选项。
I would probably code something like this (where the two loops may call the same functions)
You could also use dynamic SQL to open the cursor but that tends to get more complicated, particularly if there are only two options.
您甚至可以在隐式 for 循环中使用条件。没有游标声明或 SYS_REFCURSOR (对不起,我不喜欢它们) - 我的意思是您可以在隐式游标声明内使用您的变量,这里是
v_action
:You can even use the condition inside the implicit for loop. Without cursor declaration or
SYS_REFCURSOR
(I dislike them sorry) - I mean you can use your variables, herev_action
, inside implicit cursor declaration: