How to create stored procedure that accepts optional parameter in SQL Server?To create optional parameter in stored procedure, we set the parameter value to Show CREATE PROCEDURE [dbo].[GetAllPersonalDetails] @personalDetailsId int = null AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here IF @personalDetailsId is null BEGIN SELECT * FROM PersonalDetails END ELSE BEGIN SELECT * FROM PersonalDetails WHERE PersonalDetailsId = @personalDetailsId END END The above stored procedure may or may not be passed @personalDetailsId parameter value and it can be called in any of the following way -- Without parameter EXEC GetAllPersonalDetails -- With parameter EXEC GetAllPersonalDetails 1Views: 23940 | Post Order: 95 Related PostsI want to modify a SP in MySQL, but don't want it to crash apps currently using that SP. The SP is: SP_CreateUserReport Currently, the calling app sends something like: But I want to add a new argument to the SP: Argument 3: DemoUserEmail When I do that, and do the same call, the SP will not receive the 3rd new argument and fail. Eventually, I will update the all the calls from various apps to add the 3rd argument value, But in the meantime, I want to make that 3rd argument optional till I can update all the calling apps. And, if that value is not there, store a NULL in the table for that field. Here is a sample of the final code I wish to use.
Open in new window As a workaround approach: if you can modify the caller code to supply a document instead, you can both
Consider the following (slightly bulky) POC (full db<>fiddle)
Note the above pattern is limited to a single table pattern of the form...
...and requires modification for each table definition against which it might be applied (unless you fancy nesting dynamic SQL (which I don't)). The approach is similar to the catch-all where clause solution described in greater detail by Erland Sommarskog and Aaron Bertrand. By way of local example. If our table
...an execution of the following commands...
...would result in the following data...
Can a stored procedure have optional parameters?A parameter is considered optional if the parameter has a default value specified when it is declared. It is not necessary to provide a value for an optional parameter in a procedure call. The default value of a parameter is used when: No value for the parameter is specified in the procedure call.
What is the use of optional parameter?By definition, an Optional Parameter is a handy feature that enables programmers to pass less number of parameters to a function and assign a default value.
How do you make a stored procedure parameter optional?To create optional parameter in stored procedure, we set the parameter value to NULL while creating a stored procedure.
Can a stored procedure have no parameters?The simplest kind of SQL Server stored procedure that you can call is one that contains no parameters and returns a single result set. The Microsoft JDBC Driver for SQL Server provides the SQLServerStatement class, which you can use to call this kind of stored procedure and process the data that it returns.
|