how to select a list of 10,000 unique ids from dual in oracle SQL
Use a collection (they are not limited to 1000 items like an IN
clause is):
SELECT COLUMN_VALUE AS id
FROM TABLE(
SYS.ODCIVARCHAR2LIST(
'id123', 'id8923', 'id32983', 'id032098', 'id308230', 'id32983289'
)
)
SYS.ODCIVARCHAR2LIST
and SYS.ODCINUMBERLIST
are collection types that are supplied in the SYS
schema.
You can join this directly to whichever table you are SELECT
ing from without needing to use the DUAL
table:
SELECT y.*
FROM your_table y
INNER JOIN TABLE(
SYS.ODCIVARCHAR2LIST(
'id123', 'id8923', 'id32983', 'id032098', 'id308230', 'id32983289'
)
) i
ON (y.id = i.COLUMN_VALUE);
If you can get a collection type created then you do not even need the TABLE
expression and can use it directly in the WHERE
clause using the MEMBER OF
operator:
CREATE OR REPLACE TYPE stringlist IS TABLE OF VARCHAR2(200);
/
SELECT *
FROM yourtable
WHERE id MEMBER OF stringlist(
'id123', 'id8923', 'id32983', 'id032098', 'id308230', 'id32983289'
);
You can even pass the values as a bind parameter - see my answer here
How to put more than 1000 values into an Oracle IN clause
Put the values in a temporary table and then do a select where id in (select id from temptable)
How to get records randomly from the oracle database?
SELECT *
FROM (
SELECT *
FROM table
ORDER BY DBMS_RANDOM.RANDOM)
WHERE rownum < 21;
Binded variable to subquery
One option is to create a table, store those values into it and rewrite your query to
and a.name in (select b.name from a_new_table b)
If more than a single user uses that new table, consider using a global (or private, depending on your Oracle version) temporary table so that everyone sees only their own data, or include another identifier to serve the same purpose.
Another option is to split that loooong list of elements into rows; you'd do that in a subquery which would act just like the previous example, e..g
and a.name in (select regexp_substr(:variable, '[^,]+', 1, level) as var_name
from dual
connect by regexp_count(:variable, ',') + 1
)
Related Topics
Varchar2(N Byte|Char) Default -> Char or Byte
Distinct Random Time Generation in the Fixed Interval
How to Pass Parameter in Ado.Net Source Ssis
How to Select Top 3 Values from Each Group in a Table with SQL Which Have Duplicates
Remove Reverse Duplicates from an SQL Query
Transpose Column Headers to Rows in Postgresql
Ora-22905 - When Querying a Table Type with a Select Statement
Finding the Count of Characters and Numbers in a String
Meaning of "Select Tables Optimized Away" in MySQL Explain Plan
Bulk Insert with Variable File Name
How to Use a SQL Update Statement to Add 1 Year to a Datetime Column
How to Have Multiple Pivots Using the Same Pivot Column Using SQL Server
Using 'Like' in Ssrs Expressions
How to Copy SQL Server 2008 R2 Database from One MAChine to Another
How to Get the Latest 2 Items Per Category in One Select (With MySQL)