首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

DB2利用syscat.references递归查出他的全部关联表

2012-08-11 
DB2利用syscat.references递归查出他的所有关联表找出所有的父表:With reftables(reftabname,PRIORITY) as

DB2利用syscat.references递归查出他的所有关联表
找出所有的父表:

With reftables(reftabname,PRIORITY) as(Select reftabname,1 from syscat.references as refx where refx.tabname='PROJECT'union allSelect ref2.reftabname,reftables.PRIORITY+1 from reftables,syscat.references ref2 where reftables.reftabname=ref2.tabname)Select * from reftables ORDER BY PRIORITY DESC;


找出所有的子表
With reftables(tabname,PRIORITY) as(  Select tabname,1 from syscat.references as refx where refx.reftabname='PROJECT'  union all  Select ref2.tabname,reftables.PRIORITY+1 from reftables,syscat.references ref2 where reftables.tabname=ref2.reftabname and  reftables.PRIORITY<2)  Select distinct * from reftables ORDER BY PRIORITY DESC;

其中
reftables.PRIORITY<2
是用来控制层数,子表找到第几层!

热点排行