Solved

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

Posted on 2014-04-29
6
265 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 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

632 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