[英]user defined function call vs calling UDF in a Store procedure
I have a user defined function that is called inside a stored procedure.我有一个在存储过程中调用的用户定义函数。 All that stored procedure does is return the value that is obtained from the UDF scalar function.
该存储过程所做的只是返回从 UDF 标量函数获得的值。
However,然而,
Select * UDF_FunctionName(param1,param2)
udf call is here- udf 电话在这里-
SELECT dbo.udfFunction('1234',10) as result
and和
Exec StoreProcName(param1,param2)
are returning different results.返回不同的结果。
StoreProcName
calls the `UDF_FunctionName(param1,param2) StoreProcName
调用 `UDF_FunctionName(param1,param2)
sproc code is here- sproc 代码在这里-
BEGIN
SET NOCOUNT ON;
Return SELECT [DbName].[dbo].[udfFunction](@param1, @param2)
END
What could be the reason for different results?.不同结果的原因可能是什么?
You are trying to use RETURN
and SELECT
together:您正在尝试同时使用
RETURN
和SELECT
:
Return SELECT [DbName].[dbo].[udfFunction](@param1, @param2)
You cannot do that, you either want to return the result, or select it.你不能这样做,你要么想返回结果,要么选择它。 Depending on which you choose, retrieving the value will differ.
根据您选择的方式,检索值会有所不同。
If you do this:如果你这样做:
SELECT [DbName].[dbo].[udfFunction](@param1, @param2)
Then the resultset will have a single row and column containing your value.然后结果集将有一个包含您的值的单行和单列。 Access this exactly as you would any other resultset.
完全像访问任何其他结果集一样访问它。
If you do this:如果你这样做:
RETURN [DbName].[dbo].[udfFunction](@param1, @param2)
Your stored procedure will have a return value which is the result of your function call.您的存储过程将有一个返回值,它是您的函数调用的结果。 Access this by defining a scalar variable and assigning it to the result of the SP call - assuming the result is
INT
that might look like通过定义一个标量变量并将其分配给 SP 调用的结果来访问它 - 假设结果是
INT
可能看起来像
DECLARE @result INT
EXEC @Result = StoredProcName(@param1, @param2)
You should not use RETURN
in this way in a stored procedure.您不应在存储过程中以这种方式使用
RETURN
。 If you want to return a scalar value to the calling code use an OUTPUT
parameter .如果要向调用代码返回标量值,请使用
OUTPUT
参数。 RETURN
is generally used for status codes . RETURN
通常用于状态代码。
You might have something like你可能有类似的东西
-- SAMPLE UDF
CREATE FUNCTION dbo.YourUDF (@Username VARCHAR(30), @EntityID INT)
RETURNS INT
AS
BEGIN
RETURN @EntityID;
END
GO
-- SAMPLE PROCEDURE
CREATE PROCEDURE dbo.YourProc @Username VARCHAR(30), @EntityID INT, @Output INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SET @Output = dbo.YourUDF(@Username, @EntityID);
IF @Output IS NULL -- AN ERROR
BEGIN
RETURN 1; -- RETURN A STATUS OF -1 TO INDICATE ERROR
END
END
Then you could call this as:那么你可以称之为:
DECLARE @Output INT, @ReturnValue INT;
EXECUTE @ReturnValue = dbo.YourProc
@Username = '1234',
@EntityID = 1,
@Output = @Output OUTPUT;
SELECT ValueFromUDF = @Output,
ReturnValue = @ReturnValue;
This returns:这将返回:
ValueFromUDF ReturnValue
------------------------------
1 0
If we pass NULL
as @EntityID, to trigger the artificial error, then we get a return status of -1:如果我们将
NULL
作为 @EntityID 传递,以触发人为错误,那么我们会得到 -1 的返回状态:
DECLARE @Output INT, @ReturnValue INT;
EXECUTE @ReturnValue = dbo.YourProc
@Username = '1234',
@EntityID = NULL,
@Output = @Output OUTPUT;
SELECT ValueFromUDF = @Output,
ReturnValue = @ReturnValue;
ValueFromUDF ReturnValue
------------------------------
NULL 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.