2014-02-25 86 views

回答

39

以下是方法可以檢查depedencies

方法1:使用sp_depends可

sp_depends 'dbo.First' 
GO 

方法2:使用INFORMATION_SCHEMA.ROUTINES

SELECT * 
FROM information_schema.routines ISR 
WHERE CHARINDEX('dbo.First', ISR.ROUTINE_DEFINITION) > 0 
GO 

方法3:使用DMV sys.dm_sql_referencing_entities

SELECT referencing_schema_name, referencing_entity_name, 
referencing_id, referencing_class_desc, is_caller_dependent 
FROM sys.dm_sql_referencing_entities ('dbo.First', 'OBJECT'); 
GO 
+10

請指定這篇文章的來源,馬拉先生:http://blog.sqlauthority.com/2010/02/04/sql-server-get-the-list-of-object-dependencies-sp_depends-and-information_schema -routines-and-sys-dm_sql_referencing_entities/ – user3104183

+1

選項3是更好的選擇,因爲'sp_depends'在它的文檔中被標記爲不建議使用。 –

+0

重新創建對象時,sp_depends返回錯誤結果 – Juozas

5

查詢sysdepends表。

Select 
distinct 
schema_name(dependentObject.uid) as schema, 
dependentObject.* 
from 
    sysdepends d 
inner join sysobjects o on d.id = o.id 
inner join sysobjects dependentObject on d.depid = dependentObject.id 
where o.name = 'TableName' 

看只是景色/功能/觸發器/按名稱引用對象(或任何文本)程序的方法是:

select 
distinct schema_name(so.uid) + '.' + so.name 
from 
syscomments sc 
inner join sysobjects so on sc.id = so.id 
where sc.text like '%Name%' 
7

在SQL Server 2008中有兩個新動態管理功能引入了跟蹤對象依賴性:sys.dm_sql_referenced_entitiessys.dm_sql_referencing_entities

1 /返回引用給定實體實體:

SELECT 
     referencing_schema_name, referencing_entity_name, 
     referencing_class_desc, is_caller_dependent 
FROM sys.dm_sql_referencing_entities ('<TableName>', 'OBJECT') 

2 /返回由一個對象引用的實體:

SELECT 
     referenced_schema_name, referenced_entity_name, referenced_minor_name, 
     referenced_class_desc, is_caller_dependent, is_ambiguous 
FROM sys.dm_sql_referenced_entities ('<StoredProcedureName>', 'OBJECT'); 

或者,你可以使用sp_depends將

EXEC sp_depends '<TableName>' 

另一種選擇是使用一個非常有用的來自Red Gate的工具叫做SQL Dependency Tracker

+0

這是正確的答案。 'sp_depends'在其文檔中被標記爲已棄用。 –

+0

感謝引用和引用的依賴關係變體。 – Jaans

1

方法1:使用sp_depends將

sp_depends 'dbo.First' 
GO 

方法2:使用sys.procedures存儲過程的

select Name from sys.procedures where OBJECT_DEFINITION(OBJECT_ID) like '%Any Keyword Name%' 

'%其中一個關鍵字名稱%' 是你正在尋找

搜索關鍵字方法3:使用sys.views查看視圖

select Name from sys.views where OBJECT_DEFINITION(OBJECT_ID) like '%Any Keyword Name%' 

「%的任何關鍵字名稱%」是你正在尋找

+1

'sp_depends'在其文檔中標記爲已棄用。 @Maihai Bejenariu的答案是更好的面向未來的答案。 –

+1

sp_depends也會產生非常不準確的結果.. – Jack0fshad0ws

0
SELECT referencing_schema_name, referencing_entity_name, 
case when is_caller_dependent=0 then 'NO' ELSE 'Yes' 
END AS is_caller_dependent FROM sys.dm_sql_referencing_entities ('Tablename', 'OBJECT'); 
0

我編寫了下面的代碼,列出了找到給定關鍵字的所有對象。從形式上講,它不是一個真正的「依賴」搜索,但它有助於定位在存儲過程,視圖,觸發器和函數中使用關鍵字的位置。如果您使用動態SQL,這很有用。

select name, type_desc,create_date,modify_date from sys.all_objects o inner join sys.all_sql_modules m on m.object_id = o.object_id where definition like '%tableName %'
3

找到所有外鍵

SELECT src.name, srcCol.name, dst.name, dstCol.name 
FROM sys.foreign_key_columns fk 
    INNER JOIN sys.columns srcCol ON fk.parent_column_id = srcCol.[column_id] 
     AND fk.parent_object_id = srcCol.[object_id] 
    INNER JOIN sys.tables src ON src.[object_id] = fk.parent_object_id 
    INNER JOIN sys.tables dst ON dst.[object_id] = fk.[referenced_object_id] 
    INNER JOIN sys.columns dstCol ON fk.referenced_column_id = dstCol.[column_id] 
     AND fk.[referenced_object_id] = dstCol.[object_id] 
+1

非常感謝你,它的作用像一個魅力 –

8

除了其他答案(sp_depends系統存儲過程,SQL Server動態管理函數)中介紹的方法之外,還可以查看SSMS之間的SQL Server對象之間的依賴關係。

您可以使用SSMS的View Dependencies option。從對象資源管理器窗格中,右鍵單擊該對象並從上下文菜單中選擇視圖相依選項

我自己更喜歡被稱爲ApexSQL Search第三方依賴瀏覽器。它是一個免費的插件,集成到SSMS和Visual Studio中,用於SQL對象和數據文本搜索,擴展屬性管理,安全對象重命名和關係可視化。