我在创建返回多行的函数时遇到问题。我试图使用一个循环,但我仍然得到“oracle精确提取返回超过请求”错误。任何指导我正确方向的帮助将不胜感激。
CREATE OR REPLACE FUNCTION sections2
(
id_param NUMBER
)
RETURN VARCHAR
AS
sections_param VARCHAR(40);
BEGIN
LOOP
SELECT sections
INTO sections_param
FROM table2
WHERE id = id_param;
RETURN sections_param;
end loop;
END;
/
答案 0 :(得分:1)
听起来你想要一个返回集合的函数。我将展示一个返回嵌套表的示例
-- Use whatever length is appropriate here.
CREATE TYPE sections_tbl
IS TABLE OF VARCHAR2(40);
CREATE OR REPLACE FUNCTION sections2 (
id_param NUMBER
)
RETURN sections_tbl
AS
l_sections sections_tbl;
BEGIN
SELECT sections
BULK COLLECT INTO l_sections
FROM table2
WHERE id = id_param;
RETURN l_sections;
END;
在来电者中,您可以执行类似
的操作DECLARE
l_sections sections_tbl := sections2( <<id>> );
BEGIN
FOR i IN 1 .. l_sections.count
LOOP
dbms_output.put_line( l_sections(i) );
END LOOP;
END;