Getting error as “ORA-32044: cycle detected while executing recursive WITH query”

断了今生、忘了曾经 提交于 2019-12-11 14:36:39

问题


I am getting error as "ORA-32044: cycle detected while executing recursive WITH query" while executing the following query in 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;

回答1:


Your code will working fine except only for one data condition that is when your to_customer (1000022560394) himself have started the transaction in the first place and after some level of transaction its being returned to him only.

Eg- Sample Data Set

For this case, your Recursive part of query will find all its conditions true even at the end of the transaction, for the data will be there both in your normal table and incremental dataset.

One solution is to create a match-flag to determine its number of encounter and avoid infinite loop:

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;  


来源:https://stackoverflow.com/questions/24387957/getting-error-as-ora-32044-cycle-detected-while-executing-recursive-with-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!