这是我的职责
create or replace FUNCTION split(
i_str IN VARCHAR2,
i_delim IN VARCHAR2 DEFAULT ':'
) RETURN TABLE
AS
BEGIN
RETURN SELECT trim(regexp_substr(i_str, '[^'||i_delim||']+', 1, LEVEL)) str
FROM projet
CONNECT BY instr(i_str, i_delim, 1, LEVEL - 1) > 0;
END;
这是错误代码
PLS-00103: Encountered the symbol "SELECT" when expecting one of the following: ( - + ; case mod new not null <an identifier> <a double-quoted delimited-identifier> <a bind variable> continue avg count current exists max min prior sql stddev sum variance execute forall merge time timestamp interval date <a string literal with character set specification> <a number> <a single-quoted SQL string> pipe <an alternatively-quoted string literal with character set specification> <an altern
Errors: check compiler log
如果有人能帮我纠正这个代码,使它能正常工作
发布于 2 年前
✅ 最佳回答:
返回数据集的一种方法是使用SYS_REFCURSOR
。这种函数不能从SQL调用,但可以从编程语言(例如PL/SQL和C)调用。
CREATE OR REPLACE FUNCTION split
(
i_str IN VARCHAR2,
i_delim IN VARCHAR2 DEFAULT ':'
) RETURN SYS_REFCURSOR
AS
v_cursor SYS_REFCURSOR;
BEGIN
OPEN v_cursor FOR
SELECT trim(regexp_substr(i_str, '[^'||i_delim||']+', 1, LEVEL)) str
FROM DUAL
CONNECT BY instr(i_str, i_delim, 1, LEVEL - 1) > 0;
RETURN v_cursor;
END split;
返回数据集的另一种方法是流水线函数。为此,您需要创建一个匹配的集合类型。
CREATE OR REPLACE TYPE type_table_of_varchar2 IS TABLE OF VARCHAR2(4000);
CREATE OR REPLACE FUNCTION split
(
i_str IN VARCHAR2,
i_delim IN VARCHAR2 DEFAULT ':'
) RETURN type_table_of_varchar2 PIPELINED
AS
BEGIN
FOR rec IN
(
SELECT trim(regexp_substr(i_str, '[^'||i_delim||']+', 1, LEVEL)) AS str
FROM projet
CONNECT BY instr(i_str, i_delim, 1, LEVEL - 1) > 0
) LOOP
PIPE ROW (rec.str);
END LOOP;
END split;
可以从SQL调用流水线函数:文章来源:https://www.toymoban.com/news/detail-632934.html
SELECT * FROM TABLE( split('ABC:DEF:GHI') );
顺便说一下,我用FROM DUAL
替换了FROM projet
,因为您的查询与projet
表数据无关。文章来源地址https://www.toymoban.com/news/detail-632934.html
到了这里,关于在oracle SQL中创建返回表的函数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!