時間:2015/6/28來源:IT貓撲網(wǎng)作者:網(wǎng)管聯(lián)盟我要評論(0)
本文主要介紹了一個獲得當前數(shù)據(jù)庫對象依賴關(guān)系的實用算法,具體示例請大家參考下文:
以下為引用的內(nèi)容: create function udf_GenLevelPath() returns @v_Result table (LevelPath int,OName sysname) /****************************************************************/ /* 功能描述:按照依賴關(guān)系,列出數(shù)據(jù)庫對象 */ /* 輸入?yún)?shù):無 */ /* 輸出參數(shù):按照依賴關(guān)系排列的數(shù)據(jù)庫對象表,無依賴在前 */ /* 編寫: 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 --得到所有對象,不包括系統(tǒng)對象 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) --循環(huán)處理:由對象而得到其依賴對象 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 --調(diào)用方法 select * from dbo.udf_GenLevelPath() go |
關(guān)鍵詞標簽:數(shù)據(jù)庫
相關(guān)閱讀
熱門文章 SqlServer2005對現(xiàn)有數(shù)據(jù)進行分區(qū)具體步驟sql server系統(tǒng)表損壞的解決方法MS-SQL2005服務(wù)器登錄名、角色、數(shù)據(jù)庫用戶Access、SQL Server、Oracle常見應(yīng)用的區(qū)別
人氣排行 如何遠程備份(還原)SQL2000數(shù)據(jù)庫SQL2000數(shù)據(jù)庫遠程導(dǎo)入(導(dǎo)出)數(shù)據(jù)配置和注冊O(shè)DBC數(shù)據(jù)源-odbc數(shù)據(jù)源配置教程SQL2000和SQL2005數(shù)據(jù)庫服務(wù)端口查看或修改SQL Server 2005降級到2000的正確操作步驟修改Sql Server唯一約束教程淺談JSP JDBC來連接SQL Server 2005的方法SQL Server創(chuàng)建表語句介紹