8

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

2

4 Answers 4

8

Use a collection:

CREATE TYPE Varchar2TableType AS TABLE OF VARCHAR2(200);

Or use a built-in type like SYS.ODCIVARCHAR2LIST or SYS.ODCINUMBERLIST:

VARIABLE cursor REFCURSOR;

DECLARE
  your_collection SYS.ODCIVARCHAR2LIST := SYS.ODCIVARCHAR2LIST();
BEGIN
  your_collection.EXTEND( 100 );

  your_collection(  1) := 'Some value';
  your_collection(  2) := 'Some other value';
  -- ...
  your_collection(100) := DBMS_RANDOM.STRING( 'x', 20 );

  OPEN :cursor FOR
  SELECT t.*
  FROM   your_table t
         INNER JOIN
         TABLE( your_collection ) c
         ON t.id = c.COLUMN_VALUE;
END;
/

PRINT cursor;
Sign up to request clarification or add additional context in comments.

3 Comments

Works; thx. How can I also return the row count? I am getting a value of 0 though data exists in the cursor.
Are you using a procedure/function or an anonymous PL/SQL block?
Anonymous PL/SQL block. Thx
8

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.

Comments

3

How about using a WITH clause which basically builds a temp table? Not real reusable. You could use an array or I would argue joining to a lookup table would be better.

WITH MyListOfValues(col1) AS (
  select 'MyValue1' from dual union
  select 'MyValue2' from dual union
  select 'MyValue3' from dual 
)
SELECT * 
FROM DatabaseTable  
WHERE Column in (
  select col1
  from  MyListOfValues);

1 Comment

This requires only the privileges for the query, the other solution creates a type.
1
SELECT     REGEXP_SUBSTR ('a,b,c,d,e,f,g,h', '[^,]+', 1, LEVEL) pivot_char
      FROM DUAL
CONNECT BY REGEXP_SUBSTR ('a,b,c,d,e,f,g,h', '[^,]+', 1, LEVEL) IS NOT NULL;

I got this from:

https://www.tek-tips.com/viewthread.cfm?qid=1639911

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.