如何从另一个存储过程调用具有两个OUTPUT参数的存储过程

时间:2018-02-28 12:13:33

标签: sql sql-server

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

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

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

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

我知道如果有一个OUTPUT参数怎么做,但我不知道如何调用它所以我也得到了第二个的值。有什么想法吗?

提前致谢:)

3 个答案:

答案 0 :(得分:4)

这是一种方法:

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

location / {

        index index.php index.html index.htm;
        try_files $uri $uri/ =404; #.s. el /index.html para html5Mode de angular

        #.s. kill cache. use in dev
        sendfile off;
        add_header Last-Modified $date_gmt;
        add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
        if_modified_since off;
        expires off;
        etag off;
        proxy_no_cache 1;
        proxy_cache_bypass 1; 
    }

执行第一个的示例程序:

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

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

GO

执行第二个程序:

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

结果:

EXEC executeSumAndMultiply 3

See a live demo on rextester

答案 1 :(得分:0)

尝试按照以下方法,我给出了一个示例

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

答案 2 :(得分: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