Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL-Statement to use predefined values list as an SQL-table

Tags:

sql

list

oracle

I have a list of ids. e.g. (1, 2, 10). Is it possible to use that list as a table in Oracle ?

I mean something like that:

select * from (1, 2, 10) as x, some_table
where x not in some_table.value
like image 691
Tony Avatar asked Mar 20 '23 07:03

Tony


2 Answers

You can use with construction

with x as (
  select 1 as v from dual
  union all
  select 2 from dual
  union all
  select 10 from dual
)

select *
  from x, some_table
 where x.v not in some_table.value -- <- or whatever condition(s) required
like image 162
Dmitry Bychenko Avatar answered Mar 22 '23 20:03

Dmitry Bychenko


select to_number(regexp_substr(str, '[^,]+', 1, level)) ids
--put your comma-delimited list in the string below
  from (select '0,12,2,3,4,54,6,7,8,97' as str from dual)
connect by level <= length(regexp_replace(str, '[^,]+')) + 1
minus
--here comes select from some_table
select level from dual connect by level <= 10;
like image 38
Yaroslav Shabalin Avatar answered Mar 22 '23 20:03

Yaroslav Shabalin