如何调用具有来自另一个存储过程的两个输出参数的存储过程

时间:2022-10-20 16:39:13

Apologies if this has been asked before, but I wasn't able to find anything that worked for me.

如果之前有人问过这个问题,我很抱歉,但是我找不到任何对我有用的东西。

I have a stored procedure that has two OUTPUT parameters:

我有一个存储过程,它有两个输出参数:

CREATE PROCEDURE dbo.xxx
    .
    .
    .
    @param1 INT OUTPUT,
    @param2 INT OUTPUT 
AS

and I want to call it from another stored procedure.

我想从另一个存储过程中调用它。

I know how to do it when there is one OUTPUT parameter, but I don't know how to call it so I also get the value of the second one. Any ideas?

当有一个输出参数时,我知道怎么做,但是我不知道如何调用它,所以我也得到了第二个参数的值。什么好主意吗?

Thanks in advance :)

提前谢谢:)

3 个解决方案

#1


4  

Here is one way to do it:

有一种方法:

Sample procedure with two output parameters

带有两个输出参数的示例过程

CREATE PROCEDURE SumAndMultiply
(
    @In int,
    @OutSum int output,
    @OutMultiply int output
)
AS

    SELECT  @OutSum = @In + @In,
            @OutMultiply = @In * @In

GO

Sample procedure that executes the first one:

执行第一个步骤的示例过程:

CREATE PROCEDURE executeSumAndMultiply
(
    @In int
)
AS

DECLARE @Out1 int, 
        @Out2 int

EXEC SumAndMultiply @In = @In, @OutSum = @Out1 OUTPUT, @OutMultiply = @Out2 OUTPUT

SELECT @Out1 As Out1, @Out2 As Out2

GO

Execute the second procedure:

执行第二个程序:

EXEC executeSumAndMultiply 3

Results:

结果:

Out1    Out2
6        9

See a live demo on rextester

请查看rextester上的实时演示

#2


0  

Try to follow the below Approach, i Just given a sample example

尝试遵循下面的方法,我刚刚给出了一个示例

CREATE PROCEDURE usp_NestedSP
    @CurrentDate DATETIME OUT
AS
BEGIN
    SET @CurrentDate = GETDATE()
END
GO
--Nested SP which accepts OUTPUT parameter
CREATE PROCEDURE usp_MainSP
AS
BEGIN
    DECLARE @CurrentDate DATETIME
    EXEC [usp_NestedSP] @CurrentDate OUTPUT
    SELECT @CurrentDate AS 'ResultFromNestedSP'

END
GO

EXEc usp_MainSP

#3


0  

create procedure first_proc(@p1 int, @p2 int out, @p3 int out)
as
    set @p2 = 1;
    set @p3 = 10;
GO
create procedure second_proc
as
    declare @f1 int;
    declare @f2 int;

    exec dbo.first_proc 10, @f1 out, @f2 out;

    select 'Returned values:' t, @f1, @f2;
GO
exec dbo.second_proc;
GO
t                | (No column name) | (No column name)
:--------------- | ---------------: | ---------------:
Returned values: |                1 |               10

dbfiddle here

dbfiddle这里

#1


4  

Here is one way to do it:

有一种方法:

Sample procedure with two output parameters

带有两个输出参数的示例过程

CREATE PROCEDURE SumAndMultiply
(
    @In int,
    @OutSum int output,
    @OutMultiply int output
)
AS

    SELECT  @OutSum = @In + @In,
            @OutMultiply = @In * @In

GO

Sample procedure that executes the first one:

执行第一个步骤的示例过程:

CREATE PROCEDURE executeSumAndMultiply
(
    @In int
)
AS

DECLARE @Out1 int, 
        @Out2 int

EXEC SumAndMultiply @In = @In, @OutSum = @Out1 OUTPUT, @OutMultiply = @Out2 OUTPUT

SELECT @Out1 As Out1, @Out2 As Out2

GO

Execute the second procedure:

执行第二个程序:

EXEC executeSumAndMultiply 3

Results:

结果:

Out1    Out2
6        9

See a live demo on rextester

请查看rextester上的实时演示

#2


0  

Try to follow the below Approach, i Just given a sample example

尝试遵循下面的方法,我刚刚给出了一个示例

CREATE PROCEDURE usp_NestedSP
    @CurrentDate DATETIME OUT
AS
BEGIN
    SET @CurrentDate = GETDATE()
END
GO
--Nested SP which accepts OUTPUT parameter
CREATE PROCEDURE usp_MainSP
AS
BEGIN
    DECLARE @CurrentDate DATETIME
    EXEC [usp_NestedSP] @CurrentDate OUTPUT
    SELECT @CurrentDate AS 'ResultFromNestedSP'

END
GO

EXEc usp_MainSP

#3


0  

create procedure first_proc(@p1 int, @p2 int out, @p3 int out)
as
    set @p2 = 1;
    set @p3 = 10;
GO
create procedure second_proc
as
    declare @f1 int;
    declare @f2 int;

    exec dbo.first_proc 10, @f1 out, @f2 out;

    select 'Returned values:' t, @f1, @f2;
GO
exec dbo.second_proc;
GO
t                | (No column name) | (No column name)
:--------------- | ---------------: | ---------------:
Returned values: |                1 |               10

dbfiddle here

dbfiddle这里