sql - 如何找到导致ORA-01436的不正确数据

标签 sql oracle hierarchical-data recursive-query hierarchical-query

我有一个查询要查看数据的父子层次列表。在运行以下查询时,我得到了错误 ORA-01436

SELECT ParentPropertyRSN, CONNECT_BY_ROOT PropertyRSN as PropertyRSN,
   LEVEL, SYS_CONNECT_BY_PATH(ParentPropertyRSN, '/') Path
FROM Property
   CONNECT BY PRIOR PropertyRSN = ParentPropertyRSN
   order by level desc;

所以我在 CONNECT BY 子句中添加了 NOCYCLE 并获得了完整的数据列表及其层次路径

现在我需要一个查询来获取包含导致 ORA-01436 的不准确数据的行的列表。

最佳答案

您确实应该使用 NOCYCLE 来避免无限循环。最重要的是,您可以使用 CONNECT_BY_ISCYCLE 来识别违规行:

SELECT 
    ParentPropertyRSN, 
    CONNECT_BY_ROOT PropertyRSN as PropertyRSN,
    LEVEL, 
    SYS_CONNECT_BY_PATH(ParentPropertyRSN, '/') Path,
    CONNECT_BY_ISCYCLE Has_Cycle
FROM Property
CONNECT BY NOCYCLE PRIOR PropertyRSN = ParentPropertyRSN
ORDER BY level desc;

来自 the documentation :

The CONNECT_BY_ISCYCLE pseudocolumn returns 1 if the current row has a child which is also its ancestor. Otherwise it returns 0.

You can specify CONNECT_BY_ISCYCLE only if you have specified the NOCYCLE parameter of the CONNECT BY clause. NOCYCLE enables Oracle to return the results of a query that would otherwise fail because of a CONNECT BY loop in the data.

关于sql - 如何找到导致ORA-01436的不正确数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60435293/

相关文章:

mysql - 具有 hasMany 关系的递归查询

c# - 分组 2 个表,计算值,然后将结果保存在字典中

sql-server - Solaris 10 上的 Oracle ODBC 驱动程序

oracle - 将 "expected"Oracle 异常返回到 Java Groovy/Grails 的最佳方法

sql - 在分层 sql 中查找重复/重复的行

mysql - 平面表设计中的多级层次关系(MySql)

mysql - 检查表中已有值的问题

c# - 使用 C# 和 MySQL

oracle - 在 PL/SQL 中使用 "||"将 CLOB 与空字符串连接起来

sql - 管理分层数据 "error :REPEAT' 不是可识别的内置函数名称。”