将Varray值作为串联文本列表

发布于 2025-02-09 02:06:27 字数 1269 浏览 0 评论 0原文

在db>> for oracle 18c中:

如果我选择一个varray,那么db> gt; tifddle返回一个空的结果集,这是误导的。

with data as (select sys.odcivarchar2list('a', 'b', 'c') as my_array from dual)
select my_array from data

Result:

MY_ARRAY
--------   [resultset is empty]

php's oci_fetch_all a href =“ https://www.orafaq.com/wiki/adt” rel =“ nofollow noreferrer”>抽象数据类型喜欢 sdo_geometry,我在日志中遇到这样的错误:

  php警告:oci_fetch_all():ora-00932:不一致的数据类型:预期char获得了ADT PHP 
警告:oci_fetch_all():ora-00932:不一致的数据类型:预期char获得了ADT
 

来源:在选择sdo_geometry时返回值/行


作为解决方法,我想以串联的​​文本列表(以SQL开发人员的工作方式)返回Varray的值。

像这样:

with data as (select sys.odcivarchar2list('a', 'b', 'c') as my_array from dual)
select varray_list(my_array) from data
--          ^^^ A fake function.

Result:

MY_ARRAY
--------   
SYS.ODCIVARCHAR2LIST('a', 'b', 'c')
--or
'a', 'b', 'c'
--or 
a,b,c

问题:

在查询中,有没有办法将Varray的值作为串联文本列表?

In db<>fiddle for Oracle 18c:

If I select a varray, then db<>fiddle returns an empty resultset, which is misleading.

with data as (select sys.odcivarchar2list('a', 'b', 'c') as my_array from dual)
select my_array from data

Result:

MY_ARRAY
--------   [resultset is empty]

db<>fiddle

@JackDouglas said this in a related post:

PHP’s oci_fetch_all doesn’t like abstract data types like
SDO_GEOMETRY, I’m getting errors like this in the logs:

PHP Warning:  oci_fetch_all(): ORA-00932: inconsistent datatypes: expected CHAR got ADT PHP 
Warning:  oci_fetch_all(): ORA-00932: inconsistent datatypes: expected CHAR got ADT

Source: Return a value/row when selecting SDO_GEOMETRY


As a workaround, I want to return the varray's values as a concatenated text list (the same way it works in SQL Developer).

Like this:

with data as (select sys.odcivarchar2list('a', 'b', 'c') as my_array from dual)
select varray_list(my_array) from data
--          ^^^ A fake function.

Result:

MY_ARRAY
--------   
SYS.ODCIVARCHAR2LIST('a', 'b', 'c')
--or
'a', 'b', 'c'
--or 
a,b,c

Question:

In a query, is there a way to get a varray's values as a concatenated text list?

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

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

发布评论

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

评论(1

夏雨凉 2025-02-16 02:06:27

横向加入表收集表达式,然后使用ListAgg

with data (my_array) as (
  select sys.odcivarchar2list('a', 'b', 'c') from dual union all
  select sys.odcivarchar2list('d', 'e') from dual
)
select s.my_array_str
from   data d
       CROSS JOIN LATERAL (
         SELECT LISTAGG(column_value, ',') WITHIN GROUP (ORDER BY ROWNUM)
                  AS my_array_str
         FROM   TABLE(d.my_array)
       ) s

输出:

my_array_str
a,b,c
d,e

或,如果您想要周围的引号:

with data (my_array) as (
  select sys.odcivarchar2list('a', 'b', 'c') from dual union all
  select sys.odcivarchar2list('d', 'e') from dual union all
  select sys.odcivarchar2list('f', NULL, 'g') from dual
)
select s.my_array_str
from   data d
       CROSS JOIN LATERAL (
         SELECT LISTAGG(
                  '''' || column_value || '''',
                  ','
                ) WITHIN GROUP (ORDER BY ROWNUM)
                  AS my_array_str
         FROM   TABLE(d.my_array)
       ) s

输出:

my_array_str
'a','b','c'
'd','e'
'f','','g'

db&lt;&gt; fiddle

LATERAL join a table collection expression and then use LISTAGG:

with data (my_array) as (
  select sys.odcivarchar2list('a', 'b', 'c') from dual union all
  select sys.odcivarchar2list('d', 'e') from dual
)
select s.my_array_str
from   data d
       CROSS JOIN LATERAL (
         SELECT LISTAGG(column_value, ',') WITHIN GROUP (ORDER BY ROWNUM)
                  AS my_array_str
         FROM   TABLE(d.my_array)
       ) s

Which outputs:

MY_ARRAY_STR
a,b,c
d,e

or, if you want the surrounding quotes:

with data (my_array) as (
  select sys.odcivarchar2list('a', 'b', 'c') from dual union all
  select sys.odcivarchar2list('d', 'e') from dual union all
  select sys.odcivarchar2list('f', NULL, 'g') from dual
)
select s.my_array_str
from   data d
       CROSS JOIN LATERAL (
         SELECT LISTAGG(
                  '''' || column_value || '''',
                  ','
                ) WITHIN GROUP (ORDER BY ROWNUM)
                  AS my_array_str
         FROM   TABLE(d.my_array)
       ) s

Which outputs:

MY_ARRAY_STR
'a','b','c'
'd','e'
'f','','g'

db<>fiddle here

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