Using Input parameters in Stored Procedures


I currently have an sp that inserts the values of the input parameters into a table and that's all it does. Originally,  the sp was written to use two parameters. We now need to add a third parameter, but this may not be required in all circumstances.

Is it possible to have the 3 parameters for the sp but not use all of them. I am trying to keep  process simple by not having to write another sp for the users to run.

Any guidance would be appreciated.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ManjuIT - Project ManagerCommented:
of course yes. let me give you a logic.

sp(name, gender, age) - assuming these are the 3 parameters. off which lets say u give only name and gender. Inside SP, you need to check if you are getting input for all 3 parameters and if age is not given, give a "0" value / null value & perform the update query.
Mark WillsTopic AdvisorCommented:
you can make the new parameter effectively optional by having default = NULL

e.g. create proc my_proc (@p1 varchar(20), @p2 varchar(20) = NULL, @p3 varchar(20) = NULL)

Then @p2 and @p3 dont have to be used / supplied when calling / executing the SP.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Nitin SontakkeDeveloperCommented:
Add the the third parameter as follows:

   @third_parameter [integer] = null                     -- replace parameter name / data type as appropriate.

Open in new window

If you do this if there are call to this sp from more than one place (which it shouldn't) and if any of the calls are left unchanged, it will still work. This is because we are assigning default value to the parameter which was not sent.

If you do in fact send the value, the parameter will have it. This is best to keep the backward compatibility.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.