开发者

Assigning a value to a variable in SQL exec [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. 开发者_运维百科 Closed 10 years ago.

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
*/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜