包含引号

时间:2017-02-04 18:37:24

标签: database oracle

我想获得用逗号分隔并包含在单引号中的用户的所有类型名称。我遇到的问题是& amp;字符显示为输出而不是'。

试验1

SELECT LISTAGG(TYPE_NAME, ''',''') WITHIN GROUP (ORDER BY TYPE_NAME)
  FROM ALL_TYPES
WHERE OWNER = 'USER1';

ORA-01489:字符串连接的结果太长 01489. 00000 - "字符串连接的结果太长" *原因:字符串连接结果超过最大尺寸。 *操作:确保结果小于最大大小。

试验2

SELECT '''' || RTRIM(XMLAGG(XMLELEMENT(E,TYPE_NAME,q'$','$' ).EXTRACT('//text()')
       ORDER BY TYPE_NAME).GetClobVal(),q'$','$') AS LIST
  FROM ALL_TYPES
WHERE OWNER = 'USER1';

& TYPE1&,& TYPE2&amp ;, ...............,' TYPE3&amp ;,&

试验3

SELECT
    dbms_xmlgen.CONVERT(XMLAGG(XMLELEMENT(E,TYPE_NAME,''',''').EXTRACT('//text()')
          ORDER BY TYPE_NAME).GetClobVal())

                 AS LIST
  FROM ALL_TYPES
WHERE OWNER = 'USER1';

TYPE1& amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp;

我不想调用替换函数,然后按以下方式生成子字符串

With tbla as (
SELECT REPLACE('''' || RTRIM(XMLAGG(XMLELEMENT(E,TYPE_NAME,q'$','$' ).EXTRACT('//text()')
       ORDER BY TYPE_NAME).GetClobVal(),q'$','$'),''',''') AS LIST
  FROM ALL_TYPES
WHERE OWNER = 'USER1')
select SUBSTR(list, 1, LENGTH(list) - 2)
from tbla;

还有其他办法吗?

1 个答案:

答案 0 :(得分:0)

使用dbms_xmlgen.convert(col, 1)来阻止转义。

根据官方文档,第二个参数flag是:

  

标志

     

标志设置; ENTITY_ENCODE(默认)用于编码,和   ENTITY_DECODE用于解码。

ENTITY_DECODE - 1
ENTITY_ENCODE - 0 default

试试这个:

select 
    ''''||substr(s, 1, length(s) - 2) list
from (
    select
        dbms_xmlgen.convert(xmlagg(xmlelement(e,type_name,''',''')
              order by type_name).extract('//text()').getclobval(), 1) s
    from all_types
    where owner = 'USER1'
);

使用100000行测试下面的类似代码:

with t (s) as (
  select level
  from dual
  connect by level < 100000
)
select
  ''''||substr(s, 1, length(s) - 2)
from (
select 
  dbms_xmlgen.convert(xmlagg(XMLELEMENT(E,s,''',''') order by s desc).extract('//text()').getClobVal(), 1) s
from t);