这应该很简单,但我还没有找到真正简洁的答案。我在 sql server 中有一个非常简单的存储过程,它返回一个整数值。我想要做的就是将该返回值放入变量中以便在 Access 中使用。
存储过程:
ALTER PROCEDURE [dbo].[out_GetNextID]
@NextSumID integer
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT @NextSumID = IDENT_CURRENT('Outage Summary')+IDENT_INCR('Outage Summary')
RETURN @NextSumID
END
我正在使用ADODB来执行存储过程,我觉得问这个问题很愚蠢,但是运行cmd.Execute后如何访问Access中的返回值?提前致谢,并对这个蹩脚的问题表示歉意。
最佳答案
有两种方法,使用输出参数或设置ReturnValue
(下面讨论)。对于 OUTPUT 参数,引用此 SO link :
essentially you just need to create a SqlParameter, set the Direction to Output, and add it to the SqlCommand's Parameters collection. Then execute the stored procedure and get the value of the parameter.
查看该页面的代码。
但是,您还需要在变量声明中包含单词 OUT(或 OUTPUT):
@NextSumID integer OUT
当您将变量声明为 OUT(或 OUTPUT)时,它将自动返回,无论过程完成时它具有什么值,因此您可以只使用 RETURN。
Return Data from a Stored Procedure :MSDN
您可以使用 RETURN @NextSumID
因为您只是返回一个整数值。对于这种方法,您需要将参数指定为 ReturnValue
:
theParameter.Direction = ParameterDirection.ReturnValue
进一步讨论此方法 here (MSDN) .
关于sql-server - 将存储过程返回值分配给 VBA 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17868484/