1

I'm trying to create a function to pass a list separated with commas as a parameter to the IN clause.

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;

However, I am getting null when trying to call the following:

SELECT GET_EMPLOYEES_COUNT('''AGRICULTURE'',''IT''') FROM DUAL;
user3234428
  • 83
  • 2
  • 8

1 Answers1

0

Try:

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;
/

Live demo: http://sqlfiddle.com/#!4/a1360/1

krokodilko
  • 35,300
  • 7
  • 55
  • 79