我在代码中有下面的光标。
CURSOR cur1
IS
SELECT a, b, c, d,
FROM EMP;
BEGIN
--Stored procedure logic
END
这个 curosr 正在从 EMP 表中获取信息。
但我需要按照以下更改
- There is a table (Table1) with Key Value pairs.
- If the Table1 value is TRUE then the cursor should be created with STUDENT table
- If the table1 value is FALSE then the cursor should be created with EMP table.
我可以检查表 1 中的值,如下所示
select t.value into variable1 from Table1 t where s.key='xxxxx';
我想写一些类似的东西
IF variable1 := 'true'
curosr created with STUDENT
ELSE
curosr created with EMP
END IF
BEGIN
--Stored procedure logic
END
怎么做?
最佳答案
用另一种方式,您可以只为这两种情况保留两个 CURSORS 并在条件下打开它们。声明两个 CURSORS 不会影响性能;打开游标并从中获取数据时要小心。
PROCEDURE Get_Details_On_Condition ( name_ OUT VARCHAR2, isEmp IN BOOLEAN )
IS
CURSOR get_emp IS
SELECT name
FROM EMP;
CURSOR get_std IS
SELECT name
FROM STUDENT;
BEGIN
IF isEmp THEN
OPEN get_emp ;
FETCH get_emp INTO name_ ;
CLOSE get_emp ;
ELSE
OPEN get_std ;
FETCH get_std INTO name_ ;
CLOSE get_std ;
END IF;
RETURN name_;
END Get_Details_On_Condition;
关于sql - 带 IF 条件的 PL/SQL 游标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26144230/