从 SQL 中的存储过程获取返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13605198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 12:21:00  来源:igfitidea点击:

Get RETURN value from stored procedure in SQL

sqlsql-server

提问by hermann

I have a stored procedure where it ends with a RETURN value of 0 or 1.

我有一个存储过程,它以 RETURN 值 0 或 1 结束。

I want to use this value in an IF statement in another stored procedure.

我想在另一个存储过程的 IF 语句中使用这个值。

How can I get the return value of the former stored procedure and save it in a variable in the latter?

如何获取前一个存储过程的返回值并将其保存在后一个变量中?

I couldn't find anything related. All questions are about fetching the RETURN values in C#.

我找不到任何相关的东西。所有问题都是关于在 C# 中获取 RETURN 值。

I was thinking, maybe something like this :

我在想,也许是这样的:

SP_Two

SP_二

DECLARE @returnValue INT
SET @returnValue = EXEC SP_One

IF @returnValue = 1
BEGIN
   --do something
END
ELSE
BEGIN
   --do something else
END

回答by Rahul Tripathi

This should work for you. Infact the one which you are thinking will also work:-

这应该对你有用。事实上,您正在考虑的那个也将起作用:-

 .......
 DECLARE @returnvalue INT

 EXEC @returnvalue = SP_One
 .....

回答by Nicholas Petersen

The accepted answer is invalid with the double EXEC (only need the first EXEC):

接受的答案对双 EXEC 无效(只需要第一个 EXEC):

DECLARE @returnvalue int;
EXEC @returnvalue = SP_SomeProc
PRINT @returnvalue

And you still need to call PRINT (at least in Visual Studio).

而且您仍然需要调用 PRINT(至少在 Visual Studio 中)。

回答by Alex K.

Assign after the EXECtoken:

EXEC令牌后分配:

DECLARE @returnValue INT

EXEC @returnValue = SP_One