Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PL/SQL - Use "List" Variable in Where In Clause

Tags:

oracle

plsql

In PL/SQL, how do I declare variable MyListOfValues that contains multiple values (MyValue1, MyValue2, etc.)

SELECT * 
FROM DatabaseTable 
WHERE DatabaseTable.Field in MyListOfValues

I am using Oracle SQL Developer

like image 561
Bob Smith Avatar asked Feb 05 '16 19:02

Bob Smith


People also ask

What is Odcinumberlist?

ODCINUMBERLIST is a varying array (or varray) of NUMBER. ODCIDATELIST (10g) is an array of DATE, ODCIRAWLIST (10g) is an array of RAW(2000) and ODCIVARCHAR2LIST (10g) is an array of VARCHAR2(4000). Note the inaccurate row count! TABLE is a function that transforms the collection in a table.

Can with clause be used in cursor?

1 Answer. Show activity on this post. Yes it is possible to use with clause in cursor. Check the below example.


1 Answers

Create the SQL type like this:

CREATE TYPE MyListOfValuesType AS TABLE OF VARCHAR2(4000);

And then use it in a SQL statement

DECLARE
  MyListOfValues MyListOfValuesType;
BEGIN
  MyListOfValues := MyListOfValuesType('MyValue1', 'MyValue2');

  FOR rec IN (
    SELECT *
    FROM DatabaseTable
    WHERE DatabaseTable.Field in (
      SELECT * FROM TABLE(MyListOfValues)
    )
  )
  LOOP
    ...
  END LOOP;
END;

Up until Oracle 11g, this only works with a SQL TABLE type, not with a PL/SQL TABLE type. With Oracle 12c, you could also use PL/SQL types.

like image 77
Lukas Eder Avatar answered Oct 08 '22 17:10

Lukas Eder