default parameters can be a little weird, especially when used the way you are trying to use it.
When you want to use EXEC to run a stored procedure AND return a value, you must create a variable (in the calling routine) and pass that variable to the procedure. The problem is... passing the variable also passes the value of the variable, so the default parameter is NOT used.
Let me show you a couple examples.
Create this procedure:
Code:
Create Procedure Test_Default
@DefaultParameter int = 10
As
Select @DefaultParameter
Now test it with the following
[tt][blue]
Exec Test_Default [green]-- Returns 10[/green]
Exec Test_Default 20 [green]-- Returns 20[/green]
Exec Test_Default NULL [green]-- Returns NULL[/green]
[/blue][/tt]
So, if you pass
any value to the procedure, the default does not take affect.
Now, let's look at procedures with output parameters.
Create this procedure.
Code:
Create Procedure Test_OutputParameter
@InputParameter int,
@OutputParameter int output
As
Set @OutputParameter = @InputParameter
To test it....
[tt][blue]
Declare @Output int
Exec Test_OutputParameter 10, @Output Out
Select @Output [green]-- Returns 10[/green]
[/blue][/tt]
Note that the only way to return an output parameter is if you pass in a parameter and put OUT (or Output) on the line.
So, what have we figured out...
The only way to return an output parameter from a stored procedure is if you pass it a variable. Every variable MUSt have a value (even if the value is NULL). So, you cannot have a default output parameter and expect it to work.
There are things you can do, though. For example, you could sacrifice NULL as the 'default'. Like this...
Code:
ALTER PROCEDURE [dbo].[ap_myTest]
( @my_in int
,@my_out int output
)
AS
BEGIN
[green]-- Set the default here[/green]
Set @my_out = Coalesce(@my_out, 10)
IF @my_in = 5
BEGIN
SET @my_out = 7
END
END
The coalesce function will return the actual value for @my_out if it is NOT NULL. If it is NULL, then the value 10 will be used.
Does this make sense? If not, let me know and I will explain more.
-George
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom