檢視使用者定義函式

適用於:SQL ServerAzure SQL DatabaseAzure SQL 受控執行個體Microsoft Fabric 中的 SQL 資料庫

您可使用 SQL Server Management Studio 或 Transact-SQL,在 SQL Server 中取得的使用者定義函式的定義或屬性相關資訊。 您可能需要查看函數的定義才能了解如何從來源資料表衍生出資料;或是查看函數所定義的資料。

如果變更函數所參考的物件名稱,就必須修改該函數,使其文字反映新的名稱。 因此,在改變物件名稱時,首先要檢視此物件的相依性以了解是否有相關的函數受到影響。

權限

使用 sys.sql_expression_dependencies 該函式尋找所有相依關係需要 VIEW 對資料庫取得 DEFINITION 權限,對資料庫有 SELECT 權限 sys.sql_expression_dependencies 。 系統物件定義是公開可見的,就像 OBJECT_DEFINITION 中傳回的定義一樣。

使用 SQL Server Management Studio

顯示使用者定義函式的屬性

  1. 在物件總管中,選取資料庫旁的加號,此資料庫包含您想查看屬性的函式,然後選取加號以展開 [可程式性] 資料夾。

  2. 選擇加號以展開函式資料夾。

  3. 選取加號,展開包含您要檢視其屬性之函式的資料夾:

    • 資料表值函式
    • 純量值函式
    • 彙總函式
  4. 以滑鼠右鍵按一下要查看其屬性的函數,然後選取 [屬性]。

    下列屬性會出現在 [函式屬性 - function_name] 對話方塊中。

    函式名稱 描述
    資料庫 包含此函數之資料庫的名稱。
    伺服器 目前伺服器執行個體的名稱。
    使用者 這個連接之使用者的名稱。
    建立日期 顯示建立函數的日期。
    以...身份執行 函數的執行內容。
    名稱 目前函數的名稱。
    架構 顯示擁有函數的結構描述。
    系統物件 指出函數是否為系統物件。 值為 True 和 False。
    ANSI NULLS 指出物件是否使用 ANSI NULLS 選項建立。
    已加密 指出函數是否加密。 值為 True 和 False。
    函數類型 使用者定義函數的類型。
    帶引號的識別符 指出物件是否使用引號識別碼選項建立。
    結構描述繫結 指出函數是否綁定到結構。 值為 True 與 False。 關於結構綁定函數的資訊,請參閱 (Transact-SQL) 的 SCHEMABINDING 章節CREATE FUNCTION。

使用 Transact-SQL

取得函式的定義和屬性

  1. 在物件總管中,連線到資料庫引擎的執行個體。

  2. 在標準列上,選取 [新增查詢] 。

  3. 將下列其中一個範例複製並貼到查詢視窗中,然後按一下 [執行]。

    下列程式碼範例會取得函式名稱、定義和相關屬性。

    USE AdventureWorks2022;
    GO
    -- Get the function name, definition, and relevant properties
    SELECT sm.object_id,
       OBJECT_NAME(sm.object_id) AS object_name,
       o.type,
       o.type_desc,
       sm.definition,
       sm.uses_ansi_nulls,
       sm.uses_quoted_identifier,
       sm.is_schema_bound,
       sm.execute_as_principal_id
    -- using the two system tables sys.sql_modules and sys.objects
    FROM sys.sql_modules AS sm
    JOIN sys.objects AS o ON sm.object_id = o.object_id
    -- from the function 'dbo.ufnGetProductDealerPrice'
    WHERE sm.object_id = OBJECT_ID('dbo.ufnGetProductDealerPrice')
    ORDER BY o.type;
    GO
    

    下列程式碼範例會取得範例函式 dbo.ufnGetProductDealerPrice 的定義。

    USE AdventureWorks2022;
    GO
    -- Get the definition of the function dbo.ufnGetProductDealerPrice
    SELECT OBJECT_DEFINITION (OBJECT_ID('dbo.ufnGetProductDealerPrice')) AS ObjectDefinition;
    GO
    

如需詳細資訊,請參閱 sys.sql_modules (Transact-SQL) 和 OBJECT_DEFINITION (Transact-SQL)。

取得函式的相依性

  1. 在物件總管中,連線到資料庫引擎的執行個體。

  2. 在標準列上,選取 [新增查詢] 。

  3. 複製下列範例並將其貼到查詢視窗中,然後選取 [執行]。

    USE AdventureWorks2022;
    GO
    -- Get all of the dependency information
    SELECT OBJECT_NAME(sed.referencing_id) AS referencing_entity_name,
        o.type_desc AS referencing_description,
        COALESCE(COL_NAME(sed.referencing_id, sed.referencing_minor_id), '(n/a)') AS referencing_minor_id,
        sed.referencing_class_desc, sed.referenced_class_desc,
        sed.referenced_server_name, sed.referenced_database_name, sed.referenced_schema_name,
        sed.referenced_entity_name,
        COALESCE(COL_NAME(sed.referenced_id, sed.referenced_minor_id), '(n/a)') AS referenced_column_name,
        sed.is_caller_dependent, sed.is_ambiguous
    -- from the two system tables sys.sql_expression_dependencies and sys.object
    FROM sys.sql_expression_dependencies AS sed
    INNER JOIN sys.objects AS o ON sed.referencing_id = o.object_id
    -- on the function dbo.ufnGetProductDealerPrice
    WHERE sed.referencing_id = OBJECT_ID('dbo.ufnGetProductDealerPrice');
    GO
    

如需詳細資訊,請參閱 sys.sql_expression_dependencies (Transact-SQL) 與 sys.objects (Transact-SQL)。