在T-SQL中打印存储过程的结果

时间:2017-10-23 19:15:04

标签: sql-server tsql stored-procedures

我试图在T-SQL中使用存储过程来打印临时表中的结果,但我遇到了麻烦。

以下两个示例都会返回一个"命令已成功完成"消息,但不打印/显示任何结果。

其他线程中提出的解决方案对我来说似乎太复杂了。

任何帮助,非常感谢。

--WITH VARIABLE TEMP TABLE

CREATE PROCEDURE spTEST2
    @spSR INT
AS
    DECLARE @SR INT
    DECLARE @SR_REV MONEY
    DECLARE @T1 TABLE (ID INT IDENTITY PRIMARY KEY, SR_REV MONEY)

    INSERT INTO @T1
        SELECT SUM(OL.SalesAmount) AS SR_REV
        FROM OrderLine AS OL
        WHERE OL.SalesReasonKey = @SR

    SET @SR= @spSR 

    DECLARE @RESULT MONEY

    EXEC @RESULT = spTEST2 1

    SELECT *
    FROM @T1

    PRINT @RESULT

    DROP PROC spTEST2;
GO

--WITH TEMP TABLE
CREATE PROCEDURE spTEST
    @spSR INT
AS
    CREATE TABLE #T1 (ID INT IDENTITY PRIMARY KEY, SRKEY INT, SR_REV MONEY)

    INSERT INTO #T1
        SELECT OL.SalesReasonKey AS SRKEY, SUM(OL.SalesAmount) AS SR_REV
        FROM OrderLine AS OL
        GROUP BY OL.SalesReasonKey

    --OL.SalesReasonKey  DATA TYPE: INT
    --OL.SalesAmount DATA TYPE: MONEY

    DECLARE @SR INT  
    DECLARE @SR_REV MONEY

    SET @SR = @spSR 

    SELECT @SR_REV = SR_REV
    FROM #T1
    WHERE #T1.SRKEY = @SR

    DECLARE @SP_RESULT MONEY
    SET @SP_RESULT = @SR_REV

    EXEC @SP_RESULT = spTEST 1

    PRINT @SP_RESULT
    PRINT @SR_REV

    DROP PROC spTEST;
GO

在你的评论和共享链接之后,我修改了代码,但仍然无法正常工作 - 这次它甚至没有说成功完成了#34;:

CREATE PROC  spTEST2
@spSR  INT  ,
@spSR_REV MONEY OUTPUT

AS
SET NOCOUNT ON; 

DECLARE @SR INT
DECLARE @SR_REV MONEY
DECLARE @T1 TABLE (ID INT IDENTITY PRIMARY KEY, SRKEY INT, SR_REV MONEY)

INSERT INTO @T1
SELECT OL.SalesReasonKey AS SRKEY, SUM(OL.SalesAmount) AS SR_REV
FROM OrderLine AS OL
GROUP BY OL.SalesReasonKey
HAVING OL.SalesReasonKey = @SR

SET @SR= @spSR 
-------------------------------------------
---VERSION 1
SELECT @SR_REV  = SR_REV
FROM @T1  

SET @SR_REV = @spSR_REV
-------------------------------------------
--VERSION 2 -TRIED AS AN ALTERNATIVE TO VERSION 1
--SELECT @spSR_REV  = SR_REV
--FROM @T1  
-------------------------------------------

RETURN
GO

DECLARE @RESULT MONEY;
EXECUTE spTEST2
1  , @spSR_REV = @RESULT

PRINT @RESULT 
GO
--------------------------------------------
--FOR THE RECORD, THIS WORKS FINE AND PRINTS
CREATE PROCEDURE test3  
@SR INT,  
@SR_rev money OUTPUT  
AS    

    SET NOCOUNT ON;  
    SELECT @SR_rev = SUM( OL.SalesAmount  )
    FROM  OrderLine as OL  
    WHERE OL.SalesReasonKey = @SR;  
RETURN  
GO  

DECLARE @RESULT money;   
EXECUTE test3  
    1, @SR_rev = @RESULT OUTPUT;  

PRINT @RESULT 
GO  
------------------------------------------

1 个答案:

答案 0 :(得分:0)

你是否试图做这样的事情:

CREATE PROCEDURE spTEST2
    @spSR INT
AS
    ...
GO


 DECLARE @RESULT MONEY
 EXEC @RESULT = spTEST2 1
 PRINT @RESULT

阅读完编辑的问题后,我想我知道你需要做什么。您需要做的是首先创建所有存储过程。

CREATE spTEST1 GO //In an ideal world you would have already deployed these to you test environment.
CREATE spTEST2 GO 
CREATE spTEST3 GO

...下面...

声明三个变量,如

DECLARE 
    @Result1 MONEY, 
    @Result2 MONEY,
    @Result3 MONEY 

然后拨打电话......

EXEC @Result1 = spTEST1 1
EXEC @Result2 = spTEST1 2
EXEC @Result3 = spTEST1 3

或试试这个......

EXECUTE spTEST1 1, @SR_rev= @Result1 OUTPUT;
EXECUTE spTEST2 2, @SR_rev= @Result2 OUTPUT;
EXECUTE spTEST3 3, @SR_rev= @Result3 OUTPUT;   

...终于打印......

PRINT @Result1 
PRINT @Result2 
PRINT @Result3

......这应该有用。