In a function, I'm trying to check if a variable is in a list (this list is from a EXECUTE IMMEDIATE
), but with no success, I couldn't find the right syntax for that.
I also tried this solutions on PL/SQL - Use “List” Variable in Where In Clause, How to pass varchar with single quotes to Stored Proc in Oracle [duplicate] and How to add values to a VARRAY using a loop, but with no success.
I created the TABLE TYPE as below, but I don't know if it's the right way of doing this.
CREATE OR REPLACE TYPE DS_FUNCESP.TP_BI_LIST AS TABLE OF VARCHAR2(4000)
What I tried:
CREATE OR REPLACE FUNCTION DS_FUNCESP.FNBIGB_CheckDataMissing2
(pOwn IN VARCHAR2, pTab IN VARCHAR2, pCol IN VARCHAR2) RETURN NUMBER IS
v_str VARCHAR2(2000);
BEGIN
v_Results := DS_FUNCESP.TP_BI_LIST();
v_Qtd := 1;
v_str := ' SELECT ''TEST1'' AS NM_COLUMN FROM DUAL UNION ALL
SELECT ''TEST2'' AS NM_COLUMN FROM DUAL UNION ALL
SELECT ''TEST3'' AS NM_COLUMN FROM DUAL';
EXECUTE IMMEDIATE v_str into v_Results;
-- I tried to show the TABLE TYPE to check if I was in the right way
DBMS_OUTPUT.PUT_LINE('Total rows: '||v_Results.COUNT);
-- Check if variable is in a table/array/list (which one is the right one?)
IF pCol IN (v_Results) THEN dbms_output.put_line('YES'); ELSE dbms_output.put_line('NO');
END IF;
EXCEPTION WHEN OTHERS THEN
RETURN 0;
END FNBIGB_CheckDataMissing2;
The code above doesn't work, it gives the error:
PLS-00383: type mismatch found at 'PCOL' inside an IN or NOT IN clause
How can I do this? What is the best way?