执行用户定义函数

适用于:SQL ServerAzure SQL 数据库

使用 Transact-SQL 执行用户定义函数

限制和局限

在 Transact-SQL 中,可以使用 value 或使用 @parameter_name=value 来提供参数。参数不是事务的一部分;因此,如果在以后回退的事务中更改了参数,则此参数的值不会还原为以前的值。 返回给调用方的值总是模块返回时的值。

权限

运行 EXECUTE 语句无需权限。 但是,EXECUTE 字符串内引用的安全对象上 需要 权限。 例如,如果字符串包含 INSERT 语句,则 EXECUTE 语句的调用方必须具有对目标表的 INSERT 权限。 在遇到 EXECUTE 语句时,即使 EXECUTE 语句包含于模块内,也将检查权限。 有关更多信息,请参阅 EXECUTE (Transact-SQL)

使用 Transact-SQL

此示例中所使用的 ufnGetSalesOrderStatusText 标量值函数在大多数 AdventureWorks版本中都适用。 该函数的目的是为来自给定整数的销售状态返回文本值。 通过将整数 1 至 7 传递到 @Status 参数来更改示例。

USE [AdventureWorks2022]
GO

-- Declare a variable to return the results of the function.
DECLARE @ret nvarchar(15);

-- Execute the function while passing a value to the @status parameter
EXEC @ret = dbo.ufnGetSalesOrderStatusText @Status = 5;

-- View the returned value.  The Execute and Select statements must be executed at the same time.
SELECT N'Order Status: ' + @ret;

-- Result:
-- Order Status: Shipped

另请参阅