有条件地创建光标?

时间:2014-12-27 08:58:28

标签: oracle function plsql cursor

我想根据传递给函数的Employee ID数组来限制游标结果集,否则如果array 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:     := . ( @ % ; 

1 个答案:

答案 0 :(得分: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;
相关问题