获取错误“ORA-32044:在执行递归WITH查询时检测到循环”

时间:2014-06-24 13:29:56

标签: oracle11g

我收到错误" ORA-32044:在执行递归查询时检测到循环" 在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 个答案:

答案 0 :(得分:0)

您的代码将正常工作,但仅限于一个数据条件,即当您的to_customer(1000022560394)本身已经开始事务时,并且在某个级别的事务之后它仅返回给他。

EG- Sample Data Set

对于这种情况,即使在事务结束时,查询的递归部分也会发现它的所有条件都为真,因为数据将存在于普通表和增量数据集中。

一种解决方案是创建一个匹配标志来确定其遭遇次数并避免无限循环:

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;