檢視預存程序的相依性

適用於: SQL Server Azure SQL Database Not supported. Azure Synapse Analytics Analytics Platform System (PDW)

了解如何使用 SQL Server Management Studio 或 Transact-SQL,在 SQL Server 中檢視預存程序的相依性。

開始之前

限制事項

安全性

權限

系統函數: 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

在物件總管中檢視程序的相依性

  1. 在 [物件總管] 中,連接到資料庫引擎的執行個體,然後展開該執行個體。

  2. 依序展開 [資料庫] 、程序所屬的資料庫,以及 [可程式性]

  3. 展開 [預存程序],以滑鼠右鍵按一下程序,然後按一下 [檢視相依性]。

  4. 檢視相依於程序的物件清單。

  5. 檢視程序所相依的物件清單。

  6. 按一下 [確定]。

使用 TRANSACT-SQL

本文章需要 AdventureWorks2022 範例資料庫,您可以從 Microsoft SQL Server Samples and Community Projects (Microsoft SQL Server 範例和社群專案) 首頁下載。

在查詢編輯器中檢視程序的相依性

系統函數: sys.dm_sql_referencing_entities
此函數用來顯示相依於程序的物件。

  1. 在 [物件總管] 中,連接到資料庫引擎的執行個體,然後展開該執行個體。

  2. 展開 [資料庫] ,展開程序所屬的資料庫。

  3. 按一下 [檔案] 功能表下的 [新增查詢]

  4. 將下列範例複製並貼入查詢編輯器中。 第一個範例會建立 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  
    
  5. 建立程序之後,第二個範例會使用 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
此函數用來顯示程序所相依的物件。

  1. 在 [物件總管] 中,連接到資料庫引擎的執行個體,然後展開該執行個體。

  2. 展開 [資料庫] ,展開程序所屬的資料庫。

  3. 按一下 [檔案] 功能表下的 [新增查詢]

  4. 將下列範例複製並貼入查詢編輯器中。 第一個範例會建立 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  
    
  5. 建立程序之後,第二個範例會使用 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
此檢視可用來顯示程序相依的物件或相依於程序的物件。

顯示相依於程序的物件。

  1. 在 [物件總管] 中,連接到資料庫引擎的執行個體,然後展開該執行個體。

  2. 展開 [資料庫] ,展開程序所屬的資料庫。

  3. 按一下 [檔案] 功能表下的 [新增查詢]

  4. 將下列範例複製並貼入查詢編輯器中。 第一個範例會建立 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  
    
  5. 建立程序之後,第二個範例會使用 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_desciption,   
        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  
    

顯示程序所相依的物件。

  1. 在 [物件總管] 中,連接到資料庫引擎的執行個體,然後展開該執行個體。

  2. 展開 [資料庫] ,展開程序所屬的資料庫。

  3. 按一下 [檔案] 功能表下的 [新增查詢]

  4. 將下列範例複製並貼入查詢編輯器中。 第一個範例會建立 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  
    
  5. 建立程序之後,第二個範例會使用 sys.sql_expression_dependencies 檢視顯示程序相依的物件。

    USE AdventureWorks2022;  
    GO  
    SELECT OBJECT_NAME(referencing_id) AS referencing_entity_name,   
        o.type_desc AS referencing_desciption,   
        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)