I tried changing a default parameter value with this:
ALTER PROCEDURE [dbo].[my_sp] @currentDate datetime = GETDATE()
and all the SQL pre-compiler gave me was this error:
Msg 102, Level 15, State 1, Procedure my_sp, Line 8 Incorrect syntax near '('.
I have already created the procedure. (I'm not sure if that's relevant.) I was using a null default value and checking for that later, but that doesn't seem proper. Can I do this in one line?
[ = default ] Is a default value for the parameter. If a default value is defined, the function can be executed without specifying a value for that parameter.
Note:
Default parameter values can be specified for CLR functions except for the varchar(max) and varbinary(max) data types.When a parameter of the function has a default value, the keyword DEFAULT must be specified when the function is called to retrieve the default value. This behavior is different from using parameters with default values in stored procedures in which omitting the parameter also implies the default value.
Am I reading this wrong?
Many thanks.
Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.
If you add a parameter when creating a stored procedure, you can provide a default value so that the execution statement is not required to pass input value to this parameter. To provide a default value to a parameter, you should use this format: "@parameter_name data_type = default_value".
If you are executing a stored procedure with a bunch of parameters it can be a bit of a pain if you have to pass a value in for each of them. Fortunately, it's pretty easy to make some parameters required and others optional. You simply give them a default value.
Default value for stored procedures parameter have to be constants. You'd need to do the following...
ALTER Procedure [dbo].[my_sp] @currentDate datetime = null AS IF @currentDate is null SET @currentDate = getdate()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With