Solved

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

Posted on 2014-04-29
6
258 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

867 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