>데이터 베이스 >MySQL 튜토리얼 >如何获得当前数据库对象依赖关系_MySQL

如何获得当前数据库对象依赖关系_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB원래의
2016-06-01 14:00:371644검색

具体示例的源代码,请大家参考下文:

<p>create function udf_GenLevelPath()   <br>returns @v_Result table (LevelPath int,OName sysname)   <br>/****************************************************************/   <br>/* 功能描述:按照依赖关系,列出<u><strong><font color="#333333" size="3">数据库</font></strong></u><u><strong><font color="#333333" size="3">对象</font></strong></u> */   <br>/* 输入参数:无 */   <br>/* 输出参数:按照依赖关系排列的数据库对象表,无依赖在前 */   <br>/* 编写: anna*/   <br>/* 时间:2007-12-12 */   <br>/****************************************************************/   <br>as   <br>begin   <br>declare @vt_ObjDepPath table (LevelPath int,OName sysname null)   <br>declare @vt_Temp1 table (OName sysname null)   <br>declare @vt_Temp2 table (OName sysname null)   <br>--依赖的级别,值越小依赖性越强   <br>declare @vi_LevelPath int       <br>set @vi_LevelPath = 1   <br>--得到所有对象,不包括系统对象           <br>insert into @vt_ObjDepPath(LevelPath,OName)   <br>select @vi_LevelPath,o.name   <br>from sysobjects o   <br>where xtype not in ('S','X')   <br><br>--得到依赖对象的名称   <br>insert into @vt_Temp1(OName)   <br>select distinct object_name(sysdepends.depid)     <br>from sysdepends,@vt_ObjDepPath p   <br>where sysdepends.id  sysdepends.depid   <br>and p.OName = object_name(sysdepends.id)   <br><br>--循环处理:由对象而得到其依赖对象   <br>while (select count(*) from @vt_Temp1) > 0   <br>begin   <br>set @vi_LevelPath = @vi_LevelPath + 1   <br><br>update @vt_ObjDepPath   <br>set LevelPath = @vi_LevelPath   <br>where OName in (select OName from @vt_Temp1)   <br>and LevelPath = @vi_LevelPath - 1   <br><br>delete from @vt_Temp2   <br><br>insert into @vt_Temp2   <br>select * from @vt_Temp1   <br><br>delete from @vt_Temp1   <br><br>insert into @vt_Temp1(OName)   <br>select distinct object_name(sysdepends.depid)     <br>from sysdepends,@vt_Temp2 t2   <br>where t2.OName = object_name(sysdepends.id)   <br>and sysdepends.id  sysdepends.depid  <br><br>end       </p><p>select @vi_LevelPath = max(LevelPath) from @vt_ObjDepPath   <br><br>--修改没有依赖对象的对象级别为最大   <br>update @vt_ObjDepPath   <br>set LevelPath = @vi_LevelPath + 1   <br>where OName not in (select distinct   <br>object_name(sysdepends.id) from sysdepends)   <br>and LevelPath = 1   <br><br>insert into @v_Result   <br>select * from @vt_ObjDepPath order by LevelPath desc   <br>return   <br>end   <br>go   <br><br>--调用方法   <br>select * from dbo.udf_GenLevelPath()   <br>go</p>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.