如何在Oracle函数中返回动态结果集

时间:2010-10-22 20:42:47

标签: sql oracle function ora-00933

我在网上找到了一个字符串标记生成器查询并将其打包到下面的函数中,该函数返回动态的标记集。该函数成功编译但不知何故我收到错误“ORA-00933:SQL命令未正确结束”。有人可以帮我调试吗?谢谢。

CREATE OR REPLACE TYPE KEY_VALUE_TYPE is object (k varchar2(4000), v varchar2(4000));
CREATE OR REPLACE TYPE KEY_VALUE_TABLE is table of key_value_type;
CREATE OR REPLACE FUNCTION StrTokenizer
        (string IN VARCHAR2, delimiter IN VARCHAR2)
RETURN key_value_table AS
    v_ret key_value_table;
BEGIN
    SELECT
        CAST( 
            multiset(
            SELECT
                LEVEL  k, 
                SUBSTR(STRING_TO_TOKENIZE, 
                    DECODE(LEVEL, 1, 1, INSTR(STRING_TO_TOKENIZE, DELIMITER, 1, LEVEL-1)+1), 
                    INSTR(STRING_TO_TOKENIZE, DELIMITER, 1, LEVEL) 
                        - DECODE( LEVEL, 1, 1, INSTR(STRING_TO_TOKENIZE, DELIMITER, 1, LEVEL-1)+1)) v
            FROM
                (
                    SELECT
                        ':string'||':delimiter' AS STRING_TO_TOKENIZE , ':delimiter' AS DELIMITER
                    FROM
                        DUAL
                )
            CONNECT BY INSTR(STRING_TO_TOKENIZE, DELIMITER, 1, LEVEL)>0 
            ORDER BY level ASC) 
        As key_value_table)
    INTO
        v_ret
    FROM dual;
    return v_ret;
END;

select * from strtokenizer('a,b,c',',')
ORA-00933: SQL command not properly ended

编辑:

  1. select * from table(strtokenizer('a,b,c',','))给出“ORA-30732:表格中没有用户可见的列”。
  2. 当我以函数所有者身份登录时,
  3. ORA-30732消失,但该函数无法替换 ':string'||':delimiter',带有传入的值。我怎么能纠正这个?当我没有以其所有者身份登录时,如何运行功能?谢谢。

1 个答案:

答案 0 :(得分:1)

假设函数编译,请尝试:

SELECT * 
  FROM TABLE(strtokenizer('a,b,c',','));

参考:

该功能需要纠正 - 使用:

CREATE OR REPLACE FUNCTION StrTokenizer (string IN VARCHAR2, 
                                         delimiter IN VARCHAR2)
RETURN key_value_table AS v_ret key_value_table
BEGIN
  SELECT CAST(MULTISET(SELECT LEVEL k, 
                              SUBSTR(STRING_TO_TOKENIZE, DECODE(LEVEL, 1, 1, INSTR(STRING_TO_TOKENIZE, DELIMITER, 1, LEVEL-1)+1), 
                              INSTR(STRING_TO_TOKENIZE, DELIMITER, 1, LEVEL) - DECODE( LEVEL, 1, 1, INSTR(STRING_TO_TOKENIZE, DELIMITER, 1, LEVEL-1)+1)) v
                         FROM (SELECT string || delimiter AS STRING_TO_TOKENIZE , 
                                      delimiter AS DELIMITER
                                 FROM DUAL)
                   CONNECT BY INSTR(STRING_TO_TOKENIZE, DELIMITER, 1, LEVEL)>0 
                     ORDER BY level ASC) AS key_value_table)
    INTO v_ret
    FROM DUAL;

  RETURN v_ret;

END;