Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-04-29
6
Medium Priority
?
271 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

879 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