我得到错误
ORA-32044:在执行递归WITH查询时检测到循环
在Oracle中执行以下查询。
WITH EmpsCTE (affiliation_id, from_customer_id,to_customer_id, to_name, level1)
AS
(
SELECT affiliation_id, from_customer_id,to_customer_id, to_name, 0
FROM affiliation aff
WHERE to_customer_id != from_customer_id
and to_customer_id = 1000022560394
UNION ALL
SELECT aff.affiliation_id, aff.from_customer_id,aff.to_customer_id, aff.to_name, m.level1 + 1
FROM affiliation aff
INNER JOIN EmpsCTE m
ON aff.to_customer_id = m.from_customer_id
)
SELECT * FROM EmpsCTE;
除了一个数据条件外,您的代码将正常工作,即当您的to_customer(1000022560394)自己首先启动事务并且在某些级别的事务之后仅将其返回给他时。
- - - - - -样本数据集
在这种情况下,即使在事务结束时,查询的递归部分也会发现它的所有条件都为真,因为数据将同时存在于正常表和增量数据集中。
一个解决方案是创建一个匹配标志来确定它的相遇次数,避免无限循环:
WITH EmpsCTE (affiliation_id, from_customer_id,to_customer_id, to_name,level1,match_count)
AS
(
SELECT affiliation_id, from_customer_id,to_customer_id, to_name, 0, 0 match_count
FROM affiliation aff
WHERE to_customer_id != from_customer_id
and to_customer_id = 1000022560394
UNION ALL
SELECT aff.affiliation_id, aff.from_customer_id,aff.to_customer_id, aff.to_name, m.level1 + 1,1 match_count
FROM affiliation aff
INNER JOIN EmpsCTE m
ON aff.to_customer_id = m.from_customer_id
where m.match_count=0
)
SELECT * FROM EmpsCTE;