gpt4 book ai didi

oracle - 有条件地定义一个游标

转载 作者:行者123 更新时间:2023-12-04 10:44:35 26 4
gpt4 key购买 nike

我在 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;

最佳答案

您将需要一个 REF CURSOR 并有条件地打开它,例如:

SQL> CREATE OR REPLACE PROCEDURE GET_RECORDS(v_action IN VARCHAR2) IS
2 v_thing VARCHAR2(10);
3 get_records SYS_REFCURSOR;
4 BEGIN
5 IF (v_action = 'DO THIS') THEN
6 OPEN get_records FOR
7 SELECT 1 FROM dual;
8 ELSE
9 OPEN get_records FOR
10 SELECT 2 FROM dual;
11 END IF;
12
13 LOOP
14 FETCH get_records INTO v_thing;
15 EXIT WHEN get_records%NOTFOUND;
16 /* do things */
17 dbms_output.put_line(v_thing);
18 END LOOP;
19 CLOSE get_records;
20 END;
21 /

Procedure created

SQL> exec get_records ('DO THIS');
1

PL/SQL procedure successfully completed

SQL> exec get_records ('DO THAT');
2

PL/SQL procedure successfully completed

关于oracle - 有条件地定义一个游标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4864404/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com