• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 30
  • Last Modified:

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.

3 Solutions
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.
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now