Solved

How to ignore a SQL parameter if it is to be left blank

Posted on 2014-04-29
6
253 Views
Last Modified: 2014-04-29
I have a SQL Stored Proc and I have various parameters I pass into it, including one named @StockNbr.

I have a WHERE condition in the Stored Proc as follows:

WHERE LEFT(stk_nbr,1) IN (SELECT item Common.StrToTable(@StockNbr,100))

How do I modify the Stored Proc to ignore the @StockNbr parameter if I do not wany to pass-in a value for it?

Thanks
Fergal
0
Comment
Question by:fjkilken
6 Comments
 
LVL 8

Accepted Solution

by:
ProjectChampion earned 500 total points
ID: 40029223
You can make a parameter in a stored proc optional like this:

Create proc <myproc>
 @p0 <datatype>
, ...
, @StockNbr <datatype> = NULL
, ....

i.e. in the defition part just add "= NULL" after the datatype. After that a caller can call the proc without passing that specific parameter, or include it and explicitly pass NULL value, which will have the same effect. Inside the proc, you can check If "@StockNbr = NULL" and wither replace it with a default value of your choice or change the logic by branching to another path that doesn't need/rely on the @StockNbr.
0
 
LVL 8

Expert Comment

by:Ganapathi
ID: 40029227
You can pass the value as NULL. Or default the value to NULL while creating the procedure.

Use the below code.

WHERE
   1 = (CASE
           WHEN @StockNbr IS NULL
              THEN 1
           ELSE 2
        END)
  OR LEFT(stk_nbr,1) IN (SELECT item Common.StrToTable(@StockNbr,100))

Open in new window


If you pass a valid value for @StockNbr, then it will use it. else it will ignore the where and fetch all the data from the Table.
0
 
LVL 14

Expert Comment

by:Vikas Garg
ID: 40029228
Hi,

You can modify the SP by providing default value to the Parameter in the stored procedure where the Parameter has bee mentioned in the SP.
for example
ALTER PROCEDURE [abc]
      
      @StockNbr varcahr(30) = '1,2'

By doing this you can Execute the SP Without passing the parameter but since the SP contains Where clause the Value in the Parameter set default will affect the result.
And if the where clause is not required you can also comment that portion
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 8

Expert Comment

by:Ganapathi
ID: 40030124
The solution that I gave will not have redundant code. Isn't a good one?
0
 

Author Comment

by:fjkilken
ID: 40030203
I'm afraid the solution provided by Project Champion was the first and most intuitive one for me to use, thus the reason why I accepted it.
0
 
LVL 8

Expert Comment

by:Ganapathi
ID: 40030421
That's fine. You are the asker :)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now