我正在尝试显示我正在处理的数据库中的所有外键子项和关系(> 300个数据库),并且偶然发现了这个可以帮助我的SQL查询。
https://www.ibm.com/developerworks/data/library/techarticle/dm-1301foreignkey/index.html
我直接从上面的网站上复制了它,但它编译失败并给了我错误:
错误代码104,727
Error: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=prior;_parents)
connect by;PRIOR, DRIVER=4.15.113
SQLState: 42601
ErrorCode: -104
Error: DB2 SQL Error: SQLCODE=-727, SQLSTATE=56098, SQLERRMC=2;-104;42601;prior|_parents)
connect by|PRIOR, DRIVER=4.15.113
SQLState: 56098
ErrorCode: -727
我可以帮忙吗?谢谢。
with root_parents (root_parent_schema, root_parent_name) AS
(select tabschema, tabname
from syscat.tables
where parents = 0 and children > 0)
select
substr(connect_by_root reftabname,1,11) as root,
substr(level, 1,5) as lvl,
substr(reftabname) as parent,
substr(tabname,1,10) as child,
substr(constname,1,5) as cnst,
substr(sys_connect_by_path(reftabname, '->') || '->' ||
substr(tabname,1,20),1,42) as chain
from syscat.references
start with (reftabschema,reftabname) in (select root_parent_schema,
root_parent_name from root_parents)
connect by prior tabname = reftabname and tabschema = reftabschema;