Execute stored procedure with an Output parameter?

Viewed 843713

I have a stored procedure that I am trying to test. I am trying to test it through SQL Management Studio. In order to run this test I enter ...

exec my_stored_procedure 'param1Value', 'param2Value'

The final parameter is an output parameter. However, I do not know how to test a stored procedure with output parameters.

How do I run a stored procedure with an output parameter?

14 Answers

With this query you can execute any stored procedure (with or without an output parameter):

DECLARE @temp varchar(100)  
EXEC my_sp
    @parameter1 = 1, 
    @parameter2 = 2, 
    @parameter3 = @temp output, 
    @parameter4 = 3, 
    @parameter5 = 4
PRINT @temp

Here the datatype of @temp should be the same as @parameter3 within your Stored Procedure.

Please check below example to get output variable value by executing a stored procedure.

    DECLARE @return_value int,
    @Ouput1 int,
    @Ouput2 int,
    @Ouput3 int

EXEC    @return_value = 'Your Sp Name'
        @Param1 = value1,
        @Ouput1 = @Ouput1 OUTPUT,
        @Ouput2 = @Ouput2 OUTPUT,
        @Ouput3 = @Ouput3 OUTPUT

SELECT  @Ouput1 as N'@Ouput1',
        @Ouput2 as N'@Ouput2',
        @Ouput3 as N'@Ouput3'

Here is the definition of the stored_proc:

create proc product(@a int,@b int)
as
return @a * @b

And, this is executing it from Python: conn = pyodbc.connect('...') cursor = conn.cursor()

sql = """
SET NOCOUNT ON
declare @r float
exec @r=dbo.product 5,4
select @r
"""
result = cursor.execute(sql)
print (result.fetchall())
Related