在另一个存储过程中返回一个存储过程的输出参数

Return Output Param of a Stored Procedure inside another Stored Procedure(在另一个存储过程中返回一个存储过程的输出参数)
本文介绍了在另一个存储过程中返回一个存储过程的输出参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我试图找出在另一个存储过程中执行存储过程时如何返回存储过程的输出参数:

I'm trying to find out how to return the output parameter of a stored procedure when executing the stored procedure inside another stored procedure:

 CREATE PROCEDURE Test1

 EXEC SpWithOutputID -- Outputs @ID

 SELECT @ID as ID -- Output @ID now being used in this SP

这当然不是我的代码,只是一个例子,可以这样做吗?

This is of course not my code, but just an example, is it possible to do this?

示例 2:--这里@ID返回Null

Example 2: --Here @ID returns Null

 CREATE PROCEDURE Test1
 As 
 DECLARE @ID int

 EXEC SpWithOutputID @ID = @ID OUTPUT -- Outputs @ID

 SELECT @ID as ID -- Output @ID now being used in this SP

示例 3:--这里@ID返回一个Int

Example 3: --Here @ID returns an Int

 CREATE PROCEDURE Test1
 As 

 EXEC SpWithOutputID -- Outputs @ID

推荐答案

如果这根本不是输出参数问题,而是结果集,那么猜测 SpWithOutputID 确实是像这样(返回一个带有单行单列的 SELECT):

If this isn't really an output parameter issue at all, but rather a result set, then taking a guess that SpWithOutputID does something like this (returns a SELECT with a single row and single column):

CREATE PROCEDURE dbo.SpWithOutputID
AS
BEGIN
    SET NOCOUNT ON;

    SELECT ID = 4;
END
GO

然后 Test1 可能看起来像这样:

Then Test1 could look like this:

CREATE PROCEDURE dbo.Test1
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @ID INT;

    CREATE TABLE #x(ID INT);

    INSERT #x EXEC dbo.SpWithOutputID;

    SELECT TOP (1) @ID = ID FROM #x;

    DROP TABLE #x;
END
GO

<小时><小时>

但这对你来说是不是看起来很乱?对于单个标量值,它确实应该以这种方式工作:



But doesn't that look really messy to you? It really should work this way for single, scalar values:

CREATE PROCEDURE dbo.SpWithOutputID
    @ID INT OUTPUT 
AS
BEGIN
    SET NOCOUNT ON;

    SELECT @ID = 4; 
END 
GO

现在使用真正的输出参数要简单得多:

Now it is much simpler to consume what is really an output parameter now:

CREATE PROCEDURE dbo.Test1
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @ID INT;

    EXEC dbo.SpWithOutputID @ID = @ID OUTPUT;

    SELECT @ID;
END
GO

这篇关于在另一个存储过程中返回一个存储过程的输出参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Query with t(n) and multiple cross joins(使用 t(n) 和多个交叉连接进行查询)
Unpacking a binary string with TSQL(使用 TSQL 解包二进制字符串)
Max rows in SQL table where PK is INT 32 when seed starts at max negative value?(当种子以最大负值开始时,SQL 表中的最大行数其中 PK 为 INT 32?)
Inner Join and Group By in SQL with out an aggregate function.(SQL 中的内部连接和分组依据,没有聚合函数.)
Add a default constraint to an existing field with values(向具有值的现有字段添加默认约束)
SQL remove from running total(SQL 从运行总数中删除)