【问题标题】:Creating cursor conditionally?有条件地创建游标?
【发布时间】:2014-12-27 08:58:27
【问题描述】:

我想根据传递给函数的员工 ID 数组来限制游标结果集,否则如果数组 iks null 我想要所有记录。

这是我尝试过的东西

首先创建数组类型

    create or replace type p_emp_arr as table of number   

功能是

    create or replace
    FUNCTION getEmployee_func ( empID IN Number, empId_arr IN p_emp_arr)
    RETURN number IS
       total number(2) := 0;

      BEGIN 

      IF(empId_arr is null)
       THEN
        CURSOR empCursor IS
          SELECT * FROM Employee ;
       ELSE
        CURSOR empCursor IS
          SELECT * FROM Employee where empId in (p_emp_arr);
      END IF;

        ....
        RETURN total;
     END;

但是遇到错误

   Error(12,12): PLS-00103: Encountered the symbol "empCursor" when expecting one of the following:     := . ( @ % ; 

【问题讨论】:

  • 您应该在 PLSQL 块的声明部分声明 Explicit Cursor

标签: oracle function plsql cursor


【解决方案1】:

你可以使用REFCURSOR;

语法是这样的,

OPEN EMP_CURSOR FOR
  'SELECT * FROM Employee
       where empId in SELECT COLUMN_VALUE FROM TABLE(:empId_arr)'
   USING empId_arr ;

包含FETCH的完整块:

create or replace
FUNCTION getEmployee_func ( empID IN Number, empId_arr IN p_emp_arr)
RETURN number IS
  total number(2) := 0;
  MYREC Employee%ROWTYPE;

  EMP_CURSOR SYS_REFCURSOR;
  BEGIN 

  IF(empId_arr is null)
   THEN
    OPEN EMP_CURSOR FOR
      'SELECT * FROM Employee' ;
   ELSE
    OPEN EMP_CURSOR FOR
      'SELECT * FROM Employee
          where empId in SELECT COLUMN_VALUE FROM TABLE(:empId_arr)'
      USING empId_arr ;
  END IF;

  LOOP
     FETCH EMP_CURSOR INTO MYREC;
     EXIT WHEN EMP_CURSOR%NOTFOUND;
     .....
  END;
    ....
  RETURN total;
 END;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    • 2015-04-26
    • 2023-04-01
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 2021-12-11
    相关资源
    最近更新 更多