交叉加入到varray而不使用table()表达式

发布于 2025-02-06 17:58:22 字数 2158 浏览 0 评论 0 原文

我正在尝试了解交叉加入Varray系列。


示例1:以下查询作品。它通过交叉连接到varray列中的元素 letter_array ,使用 table()表达式:

with cte as (
select 1 as id, sys.odcivarchar2list('a', 'b')           as letter_array from dual union all
select 2 as id, sys.odcivarchar2list('c', 'd', 'e', 'f') as letter_array from dual)

select 
    cte.id,
    t.column_value
from 
    cte
cross join
    table(letter_array) t

   ID   COLUMN_VALUE
-----   -----------
    1   a    
    1   b    

    2   c    
    2   d    
    2   e    
    2   f 

它为每个< n 行生成 n 行代码> ID ( n lette_array 中的元素数量的不同数量)。


示例2:在相关文章中,我们确定 table()集合的关键字现在为 可选

因此,在下一个示例(使用略有不同的数据)中,我将使用 sys.odcivarchar2list('c','d','e',f')使用 table()表达式:

with cte as (
select 1 as id from dual union all
select 2 as id from dual)

select 
    *
from 
    cte
cross join
    sys.odcivarchar2list('c', 'd', 'e', 'f')

   ID   COLUMN_VALUE
-----   ------------
    1   c    
    1   d    
    1   e    
    1   f    

    2   c    
    2   d    
    2   e    
    2   f  

也可以按预期工作。它为每个ID产生4行(4个是硬编码Varray表达式中的元素数)。


示例3:,我实际想要的是使用第一个示例中的数据,除了这次,在JOIN中使用varray ,没有使用 table()表达式:

with cte as (
select 1 as id, sys.odcivarchar2list('a', 'b')           as letter_array from dual union all
select 2 as id, sys.odcivarchar2list('c', 'd', 'e', 'f') as letter_array from dual)

select 
    cte.id,
    t.column_value
from 
    cte
cross join
    letter_array t

错误:

ORA-00942: table or view does not exist
00942. 00000 -  "table or view does not exist"
*Cause:    
*Action:
Error at Line: 11 Column: 10

问题:

如何在不使用table()表达式的情况下交叉连接到varray?

I'm trying to learn about cross joining to varray collections.


Example 1: The following query works. It propagates rows via cross joining to the elements in the varray column letter_array, using the Table() expression:

with cte as (
select 1 as id, sys.odcivarchar2list('a', 'b')           as letter_array from dual union all
select 2 as id, sys.odcivarchar2list('c', 'd', 'e', 'f') as letter_array from dual)

select 
    cte.id,
    t.column_value
from 
    cte
cross join
    table(letter_array) t

   ID   COLUMN_VALUE
-----   -----------
    1   a    
    1   b    

    2   c    
    2   d    
    2   e    
    2   f 

It produces n rows for each ID (n is a varying number of elements in the letter_array).


Example 2: In a related post, we determined that the TABLE() keyword for collections is now optional.

So, in the next example (with slightly different data), I'll use sys.odcivarchar2list('c', 'd', 'e', 'f') hardcoded in the join, without using the TABLE() expression:

with cte as (
select 1 as id from dual union all
select 2 as id from dual)

select 
    *
from 
    cte
cross join
    sys.odcivarchar2list('c', 'd', 'e', 'f')

   ID   COLUMN_VALUE
-----   ------------
    1   c    
    1   d    
    1   e    
    1   f    

    2   c    
    2   d    
    2   e    
    2   f  

That works as expected too. It produces 4 rows for each ID (4 is the number of elements in the hardcoded varray expression).


Example 3: However, what I actually want, is to use the data from the first example, except this time, use a varray column in the join, without using the TABLE() expression:

with cte as (
select 1 as id, sys.odcivarchar2list('a', 'b')           as letter_array from dual union all
select 2 as id, sys.odcivarchar2list('c', 'd', 'e', 'f') as letter_array from dual)

select 
    cte.id,
    t.column_value
from 
    cte
cross join
    letter_array t

Error:

ORA-00942: table or view does not exist
00942. 00000 -  "table or view does not exist"
*Cause:    
*Action:
Error at Line: 11 Column: 10

Question:

How can I cross join to the varray without using the Table() expression?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走野 2025-02-13 17:58:22

使用交叉应用

with cte (id, letter_array) as (
  select 1, sys.odcivarchar2list('a', 'b')           from dual union all
  select 2, sys.odcivarchar2list('c', 'd', 'e', 'f') from dual
)
select id,
       t.column_value
from   cte
       CROSS APPLY letter_array t

输出:

id column_value
1 a
1 b
2 c
2 d
2 e
2 f

注意:使用遗产逗号加入,ANSI 交叉加入 and 交叉加入横向全部失败, Oracle 18c和21c,没有 table 关键字,然后成功。请参阅下面的小提琴。

注2:表(...)不是函数,它是

db&lt;&gt;&gt; fiddle

Use CROSS APPLY:

with cte (id, letter_array) as (
  select 1, sys.odcivarchar2list('a', 'b')           from dual union all
  select 2, sys.odcivarchar2list('c', 'd', 'e', 'f') from dual
)
select id,
       t.column_value
from   cte
       CROSS APPLY letter_array t

Which outputs:

ID COLUMN_VALUE
1 a
1 b
2 c
2 d
2 e
2 f

Note: Using a legacy comma join, ANSI CROSS JOIN and CROSS JOIN LATERAL all fail, in Oracle 18c and 21c, without the TABLE keyword and succeed with it. See the fiddle below.

Note 2: TABLE(...) is not a function, it is the syntax for a table collection expression (which wraps a collection expression).

db<>fiddle here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文