在另一个存储过程中调用存储过程时,将存储过程的输出存储到变量中

时间:2016-08-29 05:16:11

标签: sql-server stored-procedures

我正在另一个存储过程中执行存储过程,然后想要将内部存储过程的结果存储到变量中以便以后使用它。内部存储过程的结果将是varchar值。如何将变量设置为存储过程的结果?

ALTER PROCEDURE [dbo].[mvc_Formulas]    
    @Fund_ID nvarchar(max),
    @Start_Dated datetime,
    @End_Dated datetime
AS
    DECLARE @FormulaType int
    DECLARE @XFund_ID bigint

    DECLARE @ReturningTable TABLE(fundname varchar(100), zRETURN nvarchar(100))
    DECLARE @zRETURN nvarchar(100)
    DECLARE @zSD nvarchar(100)
    DECLARE @FUNDS TABLE(FundId BIGINT) 

    INSERT INTO @FUNDS
        SELECT item
        FROM dbo.SplitString(@Fund_ID, ',')

    DECLARE @MyCursor CURSOR;
    DECLARE @CurFund BIGINT;

    DECLARE @FundName NVARCHAR(100);
BEGIN
    SET @MyCursor = CURSOR FOR  
                         SELECT FundId FROM @FUNDS   

    OPEN @MyCursor 

    FETCH NEXT FROM @MyCursor INTO @CurFund

    WHILE @@FETCH_STATUS = 0
    BEGIN   
        SET @FundName = (SELECT FundName FROM FUNDS WHERE Fund_ID = @CurFund)

        --inner SP--------------------------------------
        SET @zRETURN = EXEC [dbo].[Fund_Performance_Graph] @Start_Dated, @End_Dated, @CurFund
        -----------------------------------------------------

        INSERT INTO @ReturningTable
            SELECT @FundName, @zReturn

        FETCH NEXT FROM @MyCursor INTO @CurFund 
    END

    SELECT * FROM @ReturningTable
END

1 个答案:

答案 0 :(得分:1)

ALTER PROCEDURE [dbo].[mvc_Formulas]    
    @Fund_ID nvarchar(max),
    @Start_Dated datetime,
    @End_Dated datetime,
    @Result varchar(1000) OUTPUT