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