修改用户定义函数

您可以通过使用 SQL Server Management Studio 或 Transact-SQL 修改 SQL Server 2012 中的用户定义函数。 按如下所述修改用户定义函数将不会更改函数的权限,并且也不会影响任何依赖的函数、存储过程或触发器。

本主题内容

  • 开始之前:

    限制和局限

    安全性

  • 若要修改用户定义函数,请使用:

    SQL Server Management Studio

    Transact-SQL

开始之前

限制和局限

ALTER FUNCTION 不能用于执行以下任何操作:

  • 将标量值函数更改为表值函数,反之亦然。

  • 将内联函数更改为多语句函数,反之亦然。

  • 将 Transact-SQL 函数更改为 CLR 函数,反之亦然。

安全性

权限

需要对函数或架构具有 ALTER 权限。 如果函数指定用户定义类型,则需要对该类型具有 EXECUTE 权限。

用于“返回首页”链接的箭头图标[Top]

使用 SQL Server Management Studio

修改用户定义函数

  1. 单击包含要修改的函数的数据库旁边的加号。

  2. 单击**“可编程性”**文件夹旁的加号。

  3. 单击包含要修改的函数的文件夹旁边的加号:

    • Table-valued Function

    • 标量值函数

    • 聚合函数

  4. 右键单击要修改的函数,然后单击**“修改”**。

  5. 在查询窗口中,对 ALTER FUNCTION 语句进行必要的更改。

  6. 在**“文件”菜单上,单击“保存 function_name”**。

用于“返回首页”链接的箭头图标[Top]

使用 Transact-SQL

修改用户定义函数

  1. 在**“对象资源管理器”**中,连接到数据库引擎的实例。

  2. 在标准菜单栏上,单击**“新建查询”**。

  3. 将以下示例复制并粘贴到查询窗口中,然后单击**“执行”**。

    -- Scalar-Valued Function
    USE [AdventureWorks2012]
    GO
    ALTER FUNCTION [dbo].[ufnGetAccountingEndDate]()
    RETURNS [datetime] 
    AS 
    BEGIN
        RETURN DATEADD(millisecond, -2, CONVERT(datetime, '20040701', 112));
    END;
    
    -- Table-Valued Function 
    USE [AdventureWorks2012]
    GO
    ALTER FUNCTION [dbo].[ufnGetContactInformation](@PersonID int)
    RETURNS @retContactInformation TABLE 
    (
        -- Columns returned by the function
        [PersonID] int NOT NULL, 
        [FirstName] [nvarchar](50) NULL, 
        [LastName] [nvarchar](50) NULL, 
        [JobTitle] [nvarchar](50) NULL,
        [BusinessEntityType] [nvarchar](50) NULL
    )
    AS 
    -- Returns the first name, last name, job title and business entity type for the specified contact.
    -- Since a contact can serve multiple roles, more than one row may be returned.
    BEGIN
    IF @PersonID IS NOT NULL 
    BEGIN
         IF EXISTS(SELECT * FROM [HumanResources].[Employee] e 
         WHERE e.[BusinessEntityID] = @PersonID) 
         INSERT INTO @retContactInformation
              SELECT @PersonID, p.FirstName, p.LastName, e.[JobTitle], 'Employee'
              FROM [HumanResources].[Employee] AS e
              INNER JOIN [Person].[Person] p ON p.[BusinessEntityID] = e.[BusinessEntityID]
              WHERE e.[BusinessEntityID] = @PersonID;
    
         IF EXISTS(SELECT * FROM [Purchasing].[Vendor] AS v
         INNER JOIN [Person].[BusinessEntityContact] bec ON bec.[BusinessEntityID] = v.[BusinessEntityID]
         WHERE bec.[PersonID] = @PersonID)
         INSERT INTO @retContactInformation
              SELECT @PersonID, p.FirstName, p.LastName, ct.[Name], 'Vendor Contact' 
              FROM [Purchasing].[Vendor] AS v
              INNER JOIN [Person].[BusinessEntityContact] bec ON bec.[BusinessEntityID] = v.[BusinessEntityID]
              INNER JOIN [Person].ContactType ct ON ct.[ContactTypeID] = bec.[ContactTypeID]
              INNER JOIN [Person].[Person] p ON p.[BusinessEntityID] = bec.[PersonID]
              WHERE bec.[PersonID] = @PersonID;
    
         IF EXISTS(SELECT * FROM [Sales].[Store] AS s
         INNER JOIN [Person].[BusinessEntityContact] bec ON bec.[BusinessEntityID] = s.[BusinessEntityID]
         WHERE bec.[PersonID] = @PersonID)
         INSERT INTO @retContactInformation
              SELECT @PersonID, p.FirstName, p.LastName, ct.[Name], 'Store Contact' 
              FROM [Sales].[Store] AS s
              INNER JOIN [Person].[BusinessEntityContact] bec ON bec.[BusinessEntityID] = s.[BusinessEntityID]
              INNER JOIN [Person].ContactType ct ON ct.[ContactTypeID] = bec.[ContactTypeID]
              INNER JOIN [Person].[Person] p ON p.[BusinessEntityID] = bec.[PersonID]
              WHERE bec.[PersonID] = @PersonID;
    
         IF EXISTS(SELECT * FROM [Person].[Person] AS p
         INNER JOIN [Sales].[Customer] AS c ON c.[PersonID] = p.[BusinessEntityID]
         WHERE p.[BusinessEntityID] = @PersonID AND c.[StoreID] IS NULL) 
         INSERT INTO @retContactInformation
              SELECT @PersonID, p.FirstName, p.LastName, NULL, 'Consumer' 
              FROM [Person].[Person] AS p
              INNER JOIN [Sales].[Customer] AS c ON c.[PersonID] = p.[BusinessEntityID]
              WHERE p.[BusinessEntityID] = @PersonID AND c.[StoreID] IS NULL; 
         END
    RETURN;
    END;
    

有关详细信息,请参阅 ALTER FUNCTION (Transact-SQL)

用于“返回首页”链接的箭头图标[Top]