Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-04-29
6
Medium Priority
?
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 8

Accepted Solution

by:
ProjectChampion earned 2000 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 15

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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
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

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

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

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…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

730 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