【问题标题】:Getting error as "ORA-32044: cycle detected while executing recursive WITH query"收到错误为“ORA-32044:执行递归 WITH 查询时检测到循环”
【发布时间】:2021-09-10 10:09:01
【问题描述】:

我收到错误

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;

【问题讨论】:

  • 您是否考虑过正确格式化您的代码?

标签: oracle11g


【解决方案1】:

您的代码可以正常工作,除了一个数据条件,即您的 to_customer (1000022560394) 本人首先开始交易,并且在某种程度的交易之后仅将其返回给他。

例如- 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;  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-01
    • 2015-07-20
    • 2010-12-16
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    相关资源
    最近更新 更多