檢視使用者定義函數

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

重要

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

本主題內容

開始之前

安全性

權限

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

使用 SQL Server Management Studio

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

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

  2. 按兩下加號展開 Functions 資料夾。

  3. 按下加號展開包含您要檢視屬性之函式的資料夾:

    • 資料表值函式

    • 純量值函式

    • 彙總函式

  4. 以滑鼠右鍵按一下要查看其屬性的函數,然後選取 [屬性]

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

    Database
    包含此函數之資料庫的名稱。

    Server
    目前伺服器執行個體的名稱。

    使用者
    這個連接之使用者的名稱。

    建立日期
    顯示建立函數的日期。

    執行身分
    函數的執行內容。

    名稱
    目前函數的名稱。

    結構描述
    顯示擁有函數的結構描述。

    系統物件
    指出函數是否為系統物件。 值為 True 與 False。

    ANSI NULLS
    指出物件是否使用 ANSI NULLS 選項建立。

    已加密
    指出函數是否加密。 值為 True 與 False。

    函式類型
    使用者定義函數的類型。

    引號識別碼
    指出物件是否使用引號識別碼選項建立。

    結構描述繫結
    指出函數是否為結構描述繫結函數。 值為 True 與 False。 如需結構描述繫結函式的資訊,請參閱 CREATE FUNCTION (Transact-SQL) 的 SCHEMABINDING 部分。

使用 TRANSACT-SQL

取得函式的定義和屬性

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

  2. 在標準列上,按一下 [新增查詢]

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

    USE AdventureWorks2012;  
    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  
    
    
    USE AdventureWorks2012;  
    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 AdventureWorks2012;  
    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)