具体示例的源代码,请大家参考下文:
<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> |