获得当前数据库对象依赖关系的实用算法

作者:13301 时间:2009-01-08 13:28:00 

本文主要介绍了一个获得当前数据库对象依赖关系的实用算法,具体示例请大家参考下文:


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

标签:
0
投稿

猜你喜欢

  • 百度首席设计师 用户体验部总监郭宇演讲

    2008-09-03 12:41:00
  • 《色彩解答》系列之二 色彩比例

    2008-02-17 14:38:00
  • CSS content, counter-increment 和 counter-reset详解[译]

    2009-06-02 12:51:00
  • 小议javascript设计模式

    2009-10-09 13:31:00
  • 手把手教你制作Google Sitemap

    2008-09-04 10:35:00
  • 国际上十四个优秀网页设计审核站

    2007-09-30 20:18:00
  • 如何用ASP.NET连接MS SQLServer数据库?

    2010-06-11 19:27:00
  • 学习ASP.NET八天入门:第七天

    2007-08-07 13:52:00
  • Sql Server 2000 一些安全设置

    2008-02-13 18:57:00
  • 瞎扯之Web导航

    2009-03-18 19:32:00
  • 在ASP中使用SQL语句之10:视图

    2007-08-11 13:24:00
  • 三种不同方式连接MySQL数据库的方法及示例

    2010-06-11 13:37:00
  • Window.Open详解

    2008-06-08 13:43:00
  • 指导:SQL Server无日志恢复数据库

    2009-02-20 17:07:00
  • FSO组件之文件操作(中)

    2010-05-03 11:05:00
  • asp简单的仿图片验证码

    2008-03-12 11:54:00
  • SQL优化基础 使用索引(一个小例子)

    2012-01-29 18:29:26
  • 你应当了解的5个CSS3新技术

    2009-02-11 13:01:00
  • 实现一个获取元素样式的函数getStyle

    2009-02-10 10:37:00
  • 让Dreamweaver MX显示最舒服的编程环境

    2008-02-25 14:01:00
  • asp之家 网络编程 m.aspxhome.com