查看用户定义函数

适用于:SQL ServerAzure SQL 数据库Azure 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. 选择加号可展开包含要查看属性的函数的文件夹:

    • Table-valued Function
    • 标量值函数
    • Aggregate 函数
  4. 右键单击要查看其属性的函数,然后选择“属性”

    以下属性将显示在“函数属性 - function_name”对话框

    函数名称 说明
    数据库 包含此函数的数据库的名称。
    服务器 当前服务器实例的名称。
    用户 此连接的用户名。
    创建日期 显示函数的创建日期。
    执行身份 执行该函数的上下文。
    名称 当前函数的名称。
    架构 显示函数所属的架构。
    系统对象 指示该函数是否为系统对象。 值为 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)