有没有办法在select语句中包含自定义表?
这样的事情:
select id from (1, 2, 3, 4) tbl
假设tbl
是别名,而自定义表有一列包含1,2,3,4行。
这是否可以在不创建物理临时表的情况下实现?
答案 0 :(得分:2)
是的,您可以像 Oracle 中那样(数字表):
with tbl as (
-- generates table with id column with 1, 2, 3, 4 values
select level id
from dual
connect by level <= 4)
select id
from tbl
您可以使用不同的语法:
select id
from (select level id
from dual
connect by level <= 4) tbl
如果您需要任意值,请说1, 5, 14, 127
您可以这样做:
with tbl as (
select 1 from dual
union all
select 5 from dual
union all
select 14 from dual
union all
select 127 from dual
)
select id
from tbl
或
select id
from (select 1 from dual
union all
select 5 from dual
union all
select 14 from dual
union all
select 127 from dual) tbl
如果您希望使用from (1, 2, 3)
之类的语法,则必须声明类型
create or replace type NumberTable is table of number;
...
select *
from table(NumberTable(1, 2, 3, 4)) tbl
答案 1 :(得分:0)
create or replace type char_int is table of integer;
/
with numbers as (select column_value
from table(char_int(1,2,5,6,7)))
select numbers.column_value
, object_name
, object_id
from all_objects
join numbers on (substr(object_id,1,1) = numbers.column_value);
;
输出:
<snip>
1 V$HS_AGENT 1999
2 V$HS_SESSION 2001
<snip>