getting “comma-separated list near 'xx.yy' invalid” with dbms_utility.comma_to_table

我们两清 提交于 2019-11-27 09:10:27

See How to split comma delimited string into rows

1. REGEXP_SUBSTR approach

SQL> WITH DATA AS(
  2  SELECT 'ac_Abc.88,ac_Abc.99,ac_Abc.77' str FROM dual)
  3  SELECT regexp_substr(str,'[^,]+',1,level) str
  4  FROM DATA
  5    CONNECT BY regexp_substr(str, '[^,]+', 1, level) IS NOT NULL
  6  /

STR
-----------------------------
ac_Abc.88
ac_Abc.99
ac_Abc.77

SQL>

2. XML approach

SQL> SELECT EXTRACT (VALUE (d), '//row/text()').getstringval () str
  2  FROM
  3    (SELECT XMLTYPE ( '<rows><row>'
  4      || REPLACE ('ac_Abc.88,ac_Abc.99,ac_Abc.77', ',', '</row><row>')
  5      || '</row></rows>' ) AS xmlval
  6    FROM DUAL
  7    ) x,
  8    TABLE (XMLSEQUENCE (EXTRACT (x.xmlval, '/rows/row'))) d
  9  /

STR
--------------------
ac_Abc.88
ac_Abc.99
ac_Abc.77

3. Table function

SQL> CREATE TYPE test_type
  2  AS
  3    TABLE OF VARCHAR2(100)
  4  /

Type created.

SQL>
SQL> CREATE OR REPLACE
  2  FUNCTION comma_to_table(
  3      p_list IN VARCHAR2)
  4    RETURN test_type
  5  AS
  6    l_string VARCHAR2(32767) := p_list || ',';
  7    l_comma_index PLS_INTEGER;
  8    l_index PLS_INTEGER := 1;
  9    l_tab test_type     := test_type();
 10  BEGIN
 11    LOOP
 12      l_comma_index := INSTR(l_string, ',', l_index);
 13      EXIT
 14    WHEN l_comma_index = 0;
 15      l_tab.EXTEND;
 16      l_tab(l_tab.COUNT) := SUBSTR(l_string, l_index, l_comma_index - l_index);
 17      l_index            := l_comma_index                           + 1;
 18    END LOOP;
 19    RETURN l_tab;
 20  END comma_to_table;
 21  /

Function created.

SQL> sho err
No errors.
SQL>
SQL> SELECT * FROM TABLE(comma_to_table('ac_Abc.88,ac_Abc.99,ac_Abc.77'))
  2  /

COLUMN_VALUE
--------------------------------------------------------------------------------
ac_Abc.88
ac_Abc.99
ac_Abc.77

SQL>

4. Pipelined Function

SQL> CREATE OR REPLACE
  2    FUNCTION comma_to_table(
  3        p_list IN VARCHAR2)
  4      RETURN test_type PIPELINED
  5    AS
  6      l_string LONG := p_list || ',';
  7      l_comma_index PLS_INTEGER;
  8      l_index PLS_INTEGER := 1;
  9    BEGIN
 10      LOOP
 11        l_comma_index := INSTR(l_string, ',', l_index);
 12        EXIT
 13      WHEN l_comma_index = 0;
 14        PIPE ROW ( SUBSTR(l_string, l_index, l_comma_index - l_index) );
 15        l_index := l_comma_index                           + 1;
 16      END LOOP;
 17      RETURN;
 18    END comma_to_table;
 19    /

Function created.

SQL> sho err
No errors.
SQL>
SQL> SELECT * FROM TABLE(comma_to_table('ac_Abc.88,ac_Abc.99,ac_Abc.77'))
  2  /

COLUMN_VALUE
--------------------------------------------------------------------------------
ac_Abc.88
ac_Abc.99
ac_Abc.77

It is because (Oracle doc reference)

COMMA_TO_TABLE Procedures

These procedures converts a comma-delimited list of names into a PL/SQL table of names. The second version supports fully-qualified attribute names.

A "name" referred to here is a valid Oracle (DB object) identifier, for which all naming rules apply. ac_Abc.88 is not a valid name, because in Oracle you can't have an identifier starting with a digit.

To resolve your problem with parsing strings of comma-delimited values, use the below solution of Lalit Kumar's.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!