具体示例的源代码,请大家参考下文:
create function udf_genlevelpath()
returns @v_result table (levelpath int,oname sysname)
/****************************************************************/
/* 功能描述:按照依赖关系,列出数据库对象 */
/* 输入参数:无 */
/* 输出参数:按照依赖关系排列的数据库对象表,无依赖在前 */
/* 编写: anna*/
/* 时间:2007-12-12 */
/****************************************************************/
as
begin
declare @vt_objdeppath table (levelpath int,oname sysname null)
declare @vt_temp1 table (oname sysname null)
declare @vt_temp2 table (oname sysname null)
--依赖的级别,值越小依赖性越强
declare @vi_levelpath int
set @vi_levelpath = 1
--得到所有对象,不包括系统对象
insert into @vt_objdeppath(levelpath,oname)
select @vi_levelpath,o.name
from sysobjects o
where xtype not in ('s','x')
--得到依赖对象的名称
insert into @vt_temp1(oname)
select distinct object_name(sysdepends.depid)
from sysdepends,@vt_objdeppath p
where sysdepends.id sysdepends.depid
and p.oname = object_name(sysdepends.id)
--循环处理:由对象而得到其依赖对象
while (select count(*) from @vt_temp1) > 0
begin
set @vi_levelpath = @vi_levelpath + 1
update @vt_objdeppath
set levelpath = @vi_levelpath
where oname in (select oname from @vt_temp1)
and levelpath = @vi_levelpath - 1
delete from @vt_temp2
insert into @vt_temp2
select * from @vt_temp1
delete from @vt_temp1
insert into @vt_temp1(oname)
select distinct object_name(sysdepends.depid)
from sysdepends,@vt_temp2 t2
where t2.oname = object_name(sysdepends.id)
and sysdepends.id sysdepends.depid
end
select @vi_levelpath = max(levelpath) from @vt_objdeppath
--修改没有依赖对象的对象级别为最大
update @vt_objdeppath
set levelpath = @vi_levelpath + 1
where oname not in (select distinct
object_name(sysdepends.id) from sysdepends)
and levelpath = 1
insert into @v_result
select * from @vt_objdeppath order by levelpath desc
return
end
go
--调用方法
select * from dbo.udf_genlevelpath()
go