Home > Back-end >  How can I solve problem with execute two stored procedures?
How can I solve problem with execute two stored procedures?

Time:07-01

I wanted to exec two stored procedures nested but I don't have much information about it. I created T-SQL code below, it doesn't work. Can you give me an idea? Thank you in advance.

DECLARE @currency_val decimal(18, 2);
DECLARE @first_price decimal(18, 2) = 80;
DECLARE @curr_unit nvarchar(50) = 'eur';

BEGIN
     exec sp_TLKARSILIK @first_price, @curr_unit @currency_val = @currency_val
END

BEGIN
    exec sp_DOVIZKARSILIK @currency_val, 'eur'
END

CodePudding user response:

If you want to use the output of first store procedure as input of second, it should like this:

declare @ouput1 as int

begin
    exec sp_first 'what_ever', @output1 output
    exec sp_second @output1
end

note the "output" descriptor for the @output1 parameter.

CodePudding user response:

If you mean that the value of the second SP input parameter is not correct. This is because both SPs run simultaneously. The second SP must be run after the completion of the first SP. You can use a while statement to do this.

You can test the following code:

    DECLARE @currency_val decimal(18, 2) = -1;
    DECLARE @first_price decimal(18, 2) = 80;
    DECLARE @curr_unit nvarchar(50) = 'eur';
    
    BEGIN
         exec sp_TLKARSILIK @first_price, @curr_unit @currency_val = @currency_val
    END
    
WHILE 1=1
BEGIN
IF @currency_val <> -1
BREAK
END
    BEGIN
        exec sp_DOVIZKARSILIK @currency_val, 'eur'
    END
  • Related