Capture output/Return value

  • I want to return a value (from a procedure) to it's calling procedure for error processing. Much like a function does set @Value = dbo.function.

    Create Proc...

    as

    Exec s_Proc

    IF return value = 1

    Print 'Error'

    Not sure if I can set the output of a proc to a variable in the calling procedure. Any suggestions?

  • Yes. Use this syntax

    DECLARE @return_Val INT
    
    EXEC @return_Val = s_Proc
    IF @return_val =...
    

    This will work as long as the proc uses the RETURN statement.

    SQL guy and Houston Magician

  • exec @return = Proc arg1, ...

  • Use the return statement in your procedure to return an Integer value.

    i.e.

    Create proc ReturnTest

    as

    declare @E int

     select 1/0

     set @E = @@error

    If @E <> 0

    begin

     print 'Error encountered'

     return @E

    end


    Ronald San Juan | SQL DBA
    ID 710124 ~ Code "Northwind"

  • Thanks everyone for the suggestions and quick replies. I didn't think it was that simple.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply