将逗号分隔列表作为参数传递给Oracle函数

时间:2018-05-25 17:19:34

标签: oracle function

我正在尝试创建一个函数,将以逗号分隔的列表作为参数传递给IN子句。

CREATE OR REPLACE EDITIONABLE FUNCTION "GET_EMPLOYEES_COUNT"(DEPT_STRING VARCHAR2) RETURN VARCHAR2 AS

count_emp VARCHAR2(10);

BEGIN

  select count(*) from employees where deptname IN (DEPT_STRING);

  RETURN count_emp;

END GET_EMPLOYEES_COUNT;

但是,在尝试调用以下内容时,我将变为null:

SELECT GET_EMPLOYEES_COUNT('''AGRICULTURE'',''IT''') FROM DUAL;

1 个答案:

答案 0 :(得分:0)

尝试:

CREATE OR REPLACE FUNCTION "GET_EMPLOYEES_COUNT"(DEPT_STRING VARCHAR2) 
RETURN VARCHAR2 AS

count_emp NUMBER;

BEGIN
    EXECUTE IMMEDIATE q'[DECLARE q number;
          begin  select count(*) INTO q from employees
          where deptname IN ( ]' || DEPT_STRING || q'[);
          :x := q;end;]'
    USING OUT count_emp;

   RETURN count_emp;

END GET_EMPLOYEES_COUNT;
/

现场演示:http://sqlfiddle.com/#!4/a1360/1