SQL Server 存储过程捕获 T-SQL 中的返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8540586/
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 13:40:15  来源:igfitidea点击:

SQL Server Stored Procedure capture return value in T-SQL

sqlsql-servertsql

提问by Nate Pet

I have a SQL Server stored procedure; I need to capture the return value from the stored procedure. Is this the correct way of doing this?

我有一个 SQL Server 存储过程;我需要从存储过程中捕获返回值。这是这样做的正确方法吗?

  declare valback varchar(30)
  set valback = exec storeproc1 

In this case, storeproc1is my stored procedure.

在这种情况下,storeproc1是我的存储过程。

回答by Remus Rusanu

To start, use proper T-SQL syntax:

首先,使用正确的 T-SQL 语法:

declare @valback int;
exec @valback = storeproc1;

The only return type allowed for a stored procedure is int. Stored procedures return status via the returnstatement.

存储过程允许的唯一返回类型是int. 存储过程通过return语句返回状态。

I somehow have a feeling that you really want something else, namely: to have an OUTPUT parameterin the procedure:

我不知何故有一种感觉,你真的想要别的东西,即:在过程中有一个OUTPUT 参数

declare @valback varchar(30);
exec storedproc1 @valback OUTPUT;

or capture the procedure result set via INSERT ... EXEC. See How to Share Data Between Stored Procedures.

或通过 捕获过程结果集INSERT ... EXEC。请参见如何在存储过程之间共享数据

回答by Martin

The correct syntax is:

正确的语法是:

DECLARE @valback VARCHAR(30) 
EXEC @valback = storeproc1  

As per the documentation:

根据文档:

http://msdn.microsoft.com/en-us/library/ms188332.aspx

http://msdn.microsoft.com/en-us/library/ms188332.aspx