我正在尝试将“literal”多列表用于Oracle 11g的连接查询。
这是我提出的最好的,基于from HERE( this answer建议使用更好的语法,但据我所知,它仅适用于单列表:
SELECT * from
(
-- The ugly... it burns...
select 'APPLE' as n, 'FRUIT' as m from dual
union all select 'CARROT' as n, 'VEGGIE' as m from dual
union all select 'PEACH' as n, 'FRUIT' as m from dual
union all select 'CUCUMBER' as n, 'VEGGIE' as m from dual
union all select 'ORANGE' as n, 'FRUIT' as m from dual
)
在Oracle中创建多行,多列文字表是否有一种不那么难看的方式?不幸的是,我无法创建临时表。
<rant>
正如我所预料的那样,PostgreSQL有一个this answer,但甲骨文很乱。</rant>
编辑:我曾尝试将nice, sane syntax for literal tables作为row-value constructor syntax,但这也不起作用:select n,m from dual where (n,m) in (...)
给我一个无效的标识符错误。
答案 0 :(得分:5)
对于两列,您可以使用ODCIObjectList:
select objectschema m, objectname n
from table(sys.ODCIObjectList(
sys.odciobject('APPLE', 'FRUIT'),
sys.odciobject('CARROT', 'VEGGIE'),
sys.odciobject('PEACH', 'FRUIT'),
sys.odciobject('CUCUMBER', 'VEGGIE'),
sys.odciobject('ORANGE', 'FRUIT')));
M N
---------- ----------
APPLE FRUIT
CARROT VEGGIE
PEACH FRUIT
CUCUMBER VEGGIE
ORANGE FRUIT
有关更多列,您可以定义自己的类型:
create type t as object (a varchar2(10), b varchar2(10), c number);
create type tt as table of t;
select * from table( tt (
t('APPLE', 'FRUIT', 1),
t('APPLE', 'FRUIT', 1122),
t('CARROT', 'VEGGIE', 3),
t('PEACH', 'FRUIT', 104),
t('CUCUMBER', 'VEGGIE', 5),
t('ORANGE', 'FRUIT', 6) ) )
A B C
---------- ---------- ----------
APPLE FRUIT 1
APPLE FRUIT 1122
CARROT VEGGIE 3
PEACH FRUIT 104
CUCUMBER VEGGIE 5
ORANGE FRUIT 6
答案 1 :(得分:3)
对于许多列,您可以像这样加入
SELECT a.v field1, b.v field2
FROM (SELECT column_value v, rownum r
FROM TABLE(sys.odcivarchar2list('abc', 'def', 'ghi'))) a
JOIN (SELECT column_value v, rownum r
FROM TABLE(sys.odcivarchar2list('abc', 'def', 'ghi'))) b ON a.r = b.r