適用於:SQL Server
Azure SQL Database
Azure SQL 受控執行個體
Azure Synapse Analytics
分析平台系統(PDW)
Microsoft Fabric 中的 SQL 資料庫
了解如何使用 SQL Server Management Studio 或 Transact-SQL,在 SQL Server 中檢視預存程序的相依性。
若要檢視程序的相依性,請使用:SQL Server Management Studio、Transact-SQL
開始之前
限制事項
安全性
Permissions
系統函數: sys.dm_sql_referencing_entities
需要對參考實體具有 CONTROL 權限,以及對 sys.dm_sql_referencing_entities 具有 SELECT 權限。 當受參考實體為資料分割函數時,便需要資料庫的 CONTROL 權限。 根據預設,SELECT 權限會授與 public。
系統函數: sys.dm_sql_referenced_entities
需要在 sys.dm_sql_referenced_entities 上具有 SELECT 權限,並且對參照實體具有 VIEW DEFINITION 權限。 根據預設,SELECT 權限會授與 public。 如果參考實體是資料庫層級的 DDL 觸發器,則需要 VIEW 資料庫的 DEFINITION 權限,或 ALTER DATABASE 資料庫的 DDL TRIGGER 權限。 當參考實體是伺服器層級的 DDL 觸發程序時,需要具備伺服器上的 VIEW ANY DEFINITION 權限。
物件目錄檢視: sys.sql_expression_dependencies
需要 VIEW 具有資料庫的 DEFINITION 權限,以及資料庫中的 sys.sql_expression_dependencies 的 SELECT 權限。 依預設,SELECT 權限只授與 db_owner 固定資料庫角色的成員。 當 SELECT 和 VIEW DEFINITION 權限被授予其他使用者時,受授權者可以查看資料庫中的所有相依關係。
如何檢視預存程序的相依性
您可以使用下列其中一項:
使用 SQL Server Management Studio
在物件總管中檢視程序的相依性
在「物件資源管理員」中,連接到資料庫引擎的實例,然後展開該實例。
依序展開 [資料庫] ,然後展開程序所屬的資料庫,接著展開 [可程式性] 。
展開 [預存程序],以滑鼠右鍵按一下程序,然後按一下 [檢視相依性]。
檢視相依於程序的物件清單。
檢視程序所相依的物件清單。
按一下 [確定]。
使用 TRANSACT-SQL
本文中的程式代碼範例會使用 AdventureWorks2025 或 AdventureWorksDW2025 範例資料庫,您可以從 Microsoft SQL Server 範例和社群專案 首頁下載。
在查詢編輯器中檢視程序的相依性
系統函數: sys.dm_sql_referencing_entities
此函數用來顯示相依於程序的物件。
在 [物件總管] 中,連接到資料庫引擎的執行個體,然後展開該執行個體。
展開 [資料庫] ,展開程序所屬的資料庫。
按一下 [檔案] 功能表下的 [新增查詢] 。
將下列範例複製並貼入查詢編輯器中。 第一個範例會建立
uspVendorAllInfo程序,它會傳回 Adventure Works Cycles 資料庫中的所有供應商名稱,以及他們提供的產品、他們的信用等級,以及是否能聯繫到他們。USE AdventureWorks2022; GO IF OBJECT_ID ( 'Purchasing.uspVendorAllInfo', 'P' ) IS NOT NULL DROP PROCEDURE Purchasing.uspVendorAllInfo; GO CREATE PROCEDURE Purchasing.uspVendorAllInfo WITH EXECUTE AS CALLER AS SET NOCOUNT ON; SELECT v.Name AS Vendor, p.Name AS 'Product name', v.CreditRating AS 'Rating', v.ActiveFlag AS Availability FROM Purchasing.Vendor v INNER JOIN Purchasing.ProductVendor pv ON v.BusinessEntityID = pv.BusinessEntityID INNER JOIN Production.Product p ON pv.ProductID = p.ProductID ORDER BY v.Name ASC; GO建立程序之後,第二個範例會使用 sys.dm_sql_referencing_entities 函數顯示相依於程序的物件。
USE AdventureWorks2022; GO SELECT referencing_schema_name, referencing_entity_name, referencing_id, referencing_class_desc, is_caller_dependent FROM sys.dm_sql_referencing_entities ('Purchasing.uspVendorAllInfo', 'OBJECT'); GO
系統函數: sys.dm_sql_referenced_entities
此函數用來顯示程序所相依的物件。
在 [物件總管] 中,連接到資料庫引擎的執行個體,然後展開該執行個體。
展開 [資料庫] ,展開程序所屬的資料庫。
按一下 [檔案] 功能表下的 [新增查詢] 。
將下列範例複製並貼入查詢編輯器中。 第一個範例會建立
uspVendorAllInfo程序,它會傳回 Adventure Works Cycles 資料庫中的所有供應商名稱,以及他們提供的產品、他們的信用等級,以及是否能聯繫到他們。USE AdventureWorks2022; GO IF OBJECT_ID ( 'Purchasing.uspVendorAllInfo', 'P' ) IS NOT NULL DROP PROCEDURE Purchasing.uspVendorAllInfo; GO CREATE PROCEDURE Purchasing.uspVendorAllInfo WITH EXECUTE AS CALLER AS SET NOCOUNT ON; SELECT v.Name AS Vendor, p.Name AS 'Product name', v.CreditRating AS 'Rating', v.ActiveFlag AS Availability FROM Purchasing.Vendor v INNER JOIN Purchasing.ProductVendor pv ON v.BusinessEntityID = pv.BusinessEntityID INNER JOIN Production.Product p ON pv.ProductID = p.ProductID ORDER BY v.Name ASC; GO建立程序之後,第二個範例會使用 sys.dm_sql_referenced_entities 函數顯示程序相依的物件。
USE AdventureWorks2022; GO SELECT referenced_schema_name, referenced_entity_name, referenced_minor_name,referenced_minor_id, referenced_class_desc, is_caller_dependent, is_ambiguous FROM sys.dm_sql_referenced_entities ('Purchasing.uspVendorAllInfo', 'OBJECT'); GO
物件目錄檢視: sys.sql_expression_dependencies
此檢視可用來顯示程序相依的物件或相依於程序的物件。
顯示相依於程序的物件。
在 [物件總管] 中,連接到資料庫引擎的執行個體,然後展開該執行個體。
展開 [資料庫] ,展開程序所屬的資料庫。
按一下 [檔案] 功能表下的 [新增查詢] 。
將下列範例複製並貼入查詢編輯器中。 第一個範例會建立
uspVendorAllInfo程序,它會傳回 Adventure Works Cycles 資料庫中的所有供應商名稱,以及他們提供的產品、他們的信用等級,以及是否能聯繫到他們。USE AdventureWorks2022; GO IF OBJECT_ID ( 'Purchasing.uspVendorAllInfo', 'P' ) IS NOT NULL DROP PROCEDURE Purchasing.uspVendorAllInfo; GO CREATE PROCEDURE Purchasing.uspVendorAllInfo WITH EXECUTE AS CALLER AS SET NOCOUNT ON; SELECT v.Name AS Vendor, p.Name AS 'Product name', v.CreditRating AS 'Rating', v.ActiveFlag AS Availability FROM Purchasing.Vendor v INNER JOIN Purchasing.ProductVendor pv ON v.BusinessEntityID = pv.BusinessEntityID INNER JOIN Production.Product p ON pv.ProductID = p.ProductID ORDER BY v.Name ASC; GO建立程序之後,第二個範例會使用 sys.sql_expression_dependencies 檢視顯示相依於程序的物件。
USE AdventureWorks2022; GO SELECT OBJECT_SCHEMA_NAME ( referencing_id ) AS referencing_schema_name, OBJECT_NAME(referencing_id) AS referencing_entity_name, o.type_desc AS referencing_description, COALESCE(COL_NAME(referencing_id, referencing_minor_id), '(n/a)') AS referencing_minor_id, referencing_class_desc, referenced_class_desc, referenced_server_name, referenced_database_name, referenced_schema_name, referenced_entity_name, COALESCE(COL_NAME(referenced_id, referenced_minor_id), '(n/a)') AS referenced_column_name, is_caller_dependent, is_ambiguous FROM sys.sql_expression_dependencies AS sed INNER JOIN sys.objects AS o ON sed.referencing_id = o.object_id WHERE referenced_id = OBJECT_ID(N'Purchasing.uspVendorAllInfo') GO
顯示程序所相依的物件。
在 [物件總管] 中,連接到資料庫引擎的執行個體,然後展開該執行個體。
展開 [資料庫] ,展開程序所屬的資料庫。
按一下 [檔案] 功能表下的 [新增查詢] 。
將下列範例複製並貼入查詢編輯器中。 第一個範例會建立
uspVendorAllInfo程序,它會傳回 Adventure Works Cycles 資料庫中的所有供應商名稱,以及他們提供的產品、他們的信用等級,以及是否能聯繫到他們。USE AdventureWorks2022; GO IF OBJECT_ID ( 'Purchasing.uspVendorAllInfo', 'P' ) IS NOT NULL DROP PROCEDURE Purchasing.uspVendorAllInfo; GO CREATE PROCEDURE Purchasing.uspVendorAllInfo WITH EXECUTE AS CALLER AS SET NOCOUNT ON; SELECT v.Name AS Vendor, p.Name AS 'Product name', v.CreditRating AS 'Rating', v.ActiveFlag AS Availability FROM Purchasing.Vendor v INNER JOIN Purchasing.ProductVendor pv ON v.BusinessEntityID = pv.BusinessEntityID INNER JOIN Production.Product p ON pv.ProductID = p.ProductID ORDER BY v.Name ASC; GO建立程序之後,第二個範例會使用 sys.sql_expression_dependencies 檢視顯示程序相依的物件。
USE AdventureWorks2022; GO SELECT OBJECT_NAME(referencing_id) AS referencing_entity_name, o.type_desc AS referencing_description, COALESCE(COL_NAME(referencing_id, referencing_minor_id), '(n/a)') AS referencing_minor_id, referencing_class_desc, referenced_class_desc, referenced_server_name, referenced_database_name, referenced_schema_name, referenced_entity_name, COALESCE(COL_NAME(referenced_id, referenced_minor_id), '(n/a)') AS referenced_column_name, is_caller_dependent, is_ambiguous FROM sys.sql_expression_dependencies AS sed INNER JOIN sys.objects AS o ON sed.referencing_id = o.object_id WHERE referencing_id = OBJECT_ID(N'Purchasing.uspVendorAllInfo'); GO
另請參閱
重新命名預存程序
sys.dm_sql_referencing_entities (Transact-SQL)
sys.dm_sql_referenced_entities (Transact-SQL)
sys.sql_expression_dependencies (Transact-SQL)