gpt4 book ai didi

sql - 递归选择父级的子级元素的Oracle函数错误

转载 作者:行者123 更新时间:2023-12-03 07:43:29 25 4
gpt4 key购买 nike

我正在尝试编写一个带parentID并返回给我一个“表”的oracle函数,该“表”递归地找到它的 child 和 child 的 child ,依此类推。这是我的代码,但是每当我尝试创建该函数时,我都会得到这些错误:

[错误]编译(17:10):ORA-00604:在递归SQL级别1发生错误

ORA-01422:精确获取返回的行数超过了请求的行数

ORA-06512

ORA-06512

这是代码:

CREATE OR REPLACE TYPE TAB_TableOfIDs IS TABLE OF NUMBER;

CREATE OR REPLACE FUNCTION RETURNCHILDIDS (pParentID NUMBER)
RETURN TAB_TableOfIDs
AS
ResultSet TAB_TableOfIDs;

BEGIN

WITH MY_VIEW (ID)
AS (SELECT A.ID
FROM MYTABLE A
WHERE A.ID = pParentID
UNION ALL
SELECT E.ID
FROM MYTABLE E
INNER JOIN MY_VIEW B
ON E.ParentID = B.ID)
select bulk collect into ResultSet from MY_VIEW;

RETURN ResultSet;
END;
/

有趣的是,该代码在正常的SQL查询编辑器中只能正常工作,但是如果将其放在函数中,则会出错。.那么我在做什么错呢?以及如何解决?

先感谢您。

最佳答案

我有解决方案:

CREATE OR REPLACE FUNCTION RETURNCHILDIDS  (pParentID NUMBER)
RETURN TAB_TableOfIDs
IS
CURSOR c1
IS
WITH ALT_BIRIM (targetID, ID, parentID)
AS (SELECT A.targetID, A.ID, A.PARENTID
FROM ENTKURUM A
WHERE A.ID = (SELECT ID
FROM ENTKURUM
WHERE targetID = pParentID)
UNION ALL
SELECT E.targetID, E.ID, E.PARENTID
FROM ENTKURUM E
INNER JOIN ALT_BIRIM B
ON E.PARENTID = B.ID)
SELECT targetID
FROM alt_birim;


ResultSet TAB_TableOfIDs := NEW TAB_TableOfIDs ();
BEGIN
FOR mrow IN c1
LOOP
ResultSet.EXTEND ();
ResultSet (ResultSet.COUNT) := mrow.targetID;
END LOOP;

RETURN ResultSet;
END;

关于sql - 递归选择父级的子级元素的Oracle函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24123709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com