檢視使用者定義函式

適用於:SQL ServerAzure SQL DatabaseAzure SQL 受控執行個體

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

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

權限

若要使用 sys.sql_expression_dependencies 尋找函數的所有相依性,需要資料庫的 VIEW DEFINITION 權限以及資料庫之 sys.sql_expression_dependencies 的 SELECT 權限。 系統物件定義是公開可見的,就像 OBJECT_DEFINITION 中傳回的定義一樣。

使用 SQL Server Management Studio

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

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

  2. 按一下加號展開 [函式] 資料夾。

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

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

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

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

使用 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_desciption,
        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)