I have a SQL Server stored procedure inside a multi-tenant DB. It is used by all clients, but since I don't update them all at the same time I needed to provide default parameter values so clients wouldn't throw an error if they didn't pass in the parameter.
It looks like this:
ALTER PROCEDURE [dbo].[sp_UpdateSearchValues]
@pAuthID NVARCHAR(255),
@pgKey INT,
@pSearch01 NVARCHAR(255) = 'BTR:NOSEARCHUPDATE',
@pSearch02 NVARCHAR(255) = 'BTR:NOSEARCHUPDATE',
..
@pSearch30 NVARCHAR(255) = 'BTR:NOSEARCHUPDATE'
I couldn't use null as the default value because null is a valid value to pass in (to clear out a search index). However, when I assign a parameter to DBNull.Value, it seems that the stored procedure thinks nothing is passed and the stored procedure then uses the default 'BTR:NOSEARCHUPDATE' in its logic and does nothing (instead of clearing out the value) - if I assign DBNull, the != 'BTR;NOSEARCHUPDATE' below evaluates to false.
CASE WHEN @pSearch01 != 'BTR:NOSEARCHUPDATE' THEN @pSearch01 ELSE pSearch01 END,
This statement is simply trying to get the value of the parameter if 'something was passed in (DBNull or value)', otherwise fall back to the existing value in the the corresponding db field.
I assign DBNull using the following code:
UpdateCommand.Parameters[ "@pSearch19" ].Value = DBNull.Value;
So the question is, how can I pass in 'null' to the stored procedure so that it uses that as the value instead of simply using the default value 'BTR:NOSEARCHUPDATE'?
sp_prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoidsp_and use something else as a prefix - or no prefix at all!