Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to ignore an output param of a stored procedure?

How can I ignore an output parameter of a stored procedure? I'm calling the procedure from another procedure, e.g.:

DECLARE @param1 integer

EXEC mystoredprocedure 
    @in_param_1, 
    @in_param2_, 
    @param1 OUTPUT, 
    -- what do I type here to ignore the second output param??

I'm using T-SQL (MS SQL 2005).

like image 228
srmark Avatar asked Jan 26 '09 21:01

srmark


2 Answers

You can just use NULL as the last parameter, and it should work fine - as long as that parameter isn't expected for input logic in the proc as well.

In your case, you'd call the proc as

exec mystoredproc @in_param_1, @in_param2_, @param1 OUTPUT, null

Here's another example that for the same scenario...

create proc MyTempProc
    (@one int,
    @two int out,
    @three int out)
AS
begin

    set @two = 2
    set @three = 3

    select @one as One

end
go

declare @p1 int,
        @p2 int

set     @p1 = 1

exec MyTempProc @p1, @p2 out, null 

print @p1
print @p2
like image 137
Scott Ivey Avatar answered Oct 28 '22 11:10

Scott Ivey


The output parameter has to have a default in order for you to not pass it. See below

create proc x (@in int = null, @out int = null OUTPUT)
as 
Begin
    Select @in
End

exec x 1

EDIT

To clarify a little, the error is being returned because the stored procedure that is being called has a parameter defined (in this case @param1), for which no default is defined. (i.e. @param1 int rather than @param int = -1) In the case where neither a parameter or default is specified for a defined parameter of a stored procedure when calling the stored procedure an error will occur. The same thing would happen if you tired to omit an input parameter that does not have a default specified.

like image 33
cmsjr Avatar answered Oct 28 '22 12:10

cmsjr