2012-04-02 74 views
4

在過去,我想在右鍵單擊SSMS中的表並單擊'查看依賴關係'時,它會列出所有使用該表的表和存儲特效。但最近我注意到它錯過了一些依賴關係。SQL管理工作室'查看依賴關係'不列出所有依賴關係

有時它會遺漏一些使用SELECT語句引用它的存儲過程,有時它是INSERT或UPDATE語句。關於什麼是依賴的,什麼不是,似乎沒有共同的模式。任何人都可以看到這一點嗎? (不,這不是一個許可問題,我有系統管理員權限)

我最終的目標是找到一種方法來找到所有依賴關係的特定表/視圖/存儲過程使用最簡單的方法(即。沒有腳本),但它必須返回所有的依賴關係,而不僅僅是一些。任何幫助?

PS。我正在使用SQL Server 2005和SSMS 2005.

謝謝。

+3

[我知道這是2008年,但它有許多有用的信息](http://sqlblog.com/blogs/aaron_bertrand/archive/2008/09/09/keeping-sysdepends-up-to- date-in-sql-server-2008.aspx) – 2012-04-02 01:53:47

回答

9

這是SQL Server的「已知」問題。實際上,這個SSMS功能使用起來很危險,特別是當你想要找出你必須改變哪些對象時,如果你對Table或View做了一些改變。

而且 - 不使用腳本在syscomments中進行搜索,就好像對象文本是超過8000個符號則會將其分割成幾個部分,讓你的表的那名可以切割成

RECORD1: ...... table_that_yo

RECORD2:u_search ........

此前我的「食譜」是所有腳本對象轉換成文本文件到硬盤,並執行「在文件中搜索」使用任何文本編輯器 - 就像記事本++(查找文件)功能的SSMS一樣。

我現在已經創建了一個腳本,允許在對象定義中使用的搜索引擎內置SQL函數:

/* 
This is an easy way to look through the sources of all objects in the database 
if you need to find particular string. This script can be used, for example, 
to find references of some specific object by other objects. Depending on the 
size of your database you might want to limit the search scope to particular 
object type. Just comment unneeded object types in WHERE statement. 
Enter search string between %% marks in @SearchPattern initialisation statement. 
When you get the results you can copy object name from "FullName" column and 
use SSMSBoost to quickly locate it in the object explorer, or you can continue 
searching in results using "Find in ResultsGrid" function. 

This script is provided to you by SSMSBoost add-in team as is. Improvements and 
comments are welcome. 
Redistribution with reference to SSMSBoost project website is welcome. 
SSMSBoost team, 2014 
*/ 

DECLARE @SearchPattern NVARCHAR(128) 

SET @SearchPattern = '%%' 

SELECT SCHEMA_NAME(o.schema_id) as [schema] 
,  o.[name] 
,  o.[type] 
,  '['+SCHEMA_NAME(o.schema_id)+'].['+o.[name]+']' as FullName 
,  OBJECT_DEFINITION(object_id) AS [Source] 
FROM sys.objects AS o 
WHERE lower(OBJECT_DEFINITION(o.object_id)) LIKE lower(@SearchPattern) 
    AND o.[type] IN (
    'C',--- = Check constraint 
    'D',--- = Default (constraint or stand-alone) 
    'P',--- = SQL stored procedure 
    'FN',--- = SQL scalar function 
    'R',--- = Rule 
    'RF',--- = Replication filter procedure 
    'TR',--- = SQL trigger (schema-scoped DML trigger, or DDL trigger at either the database or server scope) 
    'IF',--- = SQL inline table-valued function 
    'TF',--- = SQL table-valued function 
    'V') --- = View 
ORDER BY o.[type] 
,  o.[name] 
+0

我很害怕它會來... ...但謝謝你確認我的疑惑。 – 2012-04-03 23:42:36

+0

我已經創建了基於OBJECT_DEFINITION()函數的SQL腳本,該函數可用於直接從SSMS搜索依賴項,而無需將對象編寫爲文本文件。腳本位於:http://www.ssmsboost.com/social/yaf_postst123_Search-in-Sources-sis--Autoreplacement.aspx – 2014-05-08 16:51:24

+1

看起來不錯。你爲什麼不更新你的答案來包含腳本?對於這個問題的其他訪問者來說,這將是一個很好的參考。 :) – 2014-05-08 22:48:46

1

我剛纔注意到了同樣的問題,與管理Studio。

但幸運的是發現如果您使用Visual Studio Database projects,並使用「查看架構」窗格,您可以右鍵點擊一個項目並查看依賴關係。顯示的依賴關係似乎是準確的。

+0

我不得不從SQL Server對象資源管理器中的數據庫中創建一個項目,然後單擊所需的對象,然後在T-SQL視圖中選擇我想要查找引用的列或表,然後單擊查找所有參考文獻,它找到所有參考文獻做得非常好。謝謝 – 2013-12-05 00:04:25