It's possible to assign a value to a variable returned from exec @sql
?:
set @var= exec calculate 'param' are this true
DECLARE @ret INT
DECLARE @output INT
EXEC @ret = [proc] @input, @output OUTPUT
SELECT @ret, @output
@ret
is the return value: RETURN -1
@output
is an assignable variable of any type: SET @output = 123
You should be using output parameters for the stored procedure:
DECLARE @output1 INT
EXEC [proc] @input, @output1 OUTPUT
PRINT @output1
A function is a good place to perform a commonly used calculation.
CREATE FUNCTION dbo.ufnAddIntegers
(@pint1 as int, @pint2 as int)
RETURNS int
AS
BEGIN
return @pint1 + @pint2
END
go
declare @intResult int
set @intResult = dbo.ufnAddIntegers(3, 4)
select Result = @intResult
/*
Result
-----------
7
*/
精彩评论