devxlogo

Optional Parameters in SQL

Optional Parameters in SQL

You may not know this but you can have optional Parameters in SQL. It took me forever to figure it out.

In any case, you can implement an optional parameter by declaring a parameter in your stored procedure and giving it a default value of NULL, then in your WHERE clause, you just do a check to see if the parameter (with the NULL value) is NULL. An example follows:

 CREATE PROCEDURE GetNameS@LastName VARCHAR(50) = NULL --Creating Optional ParameterASSELECT * FROM PersonalInfoWHERE (LastName = @LastName OR @LastName IS NULL)

In the above query I tested to see if the LastName field equals the supplied value or if the supplied value (the optional parameter) IS NULL.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist