473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Changing Where Clause Based on Parameter

Hi Folks,

I have a parameter in a stored proc that can be null, a single value or a list of values delimited by commas, here's my problem

If the value is null I want the field to be any value but if it's not I want it to be in the list, how to I put that in the where clause?

The logic is something like

if @var is null then
EnterpriseId > 0
else EnterpriseId in (@var)
Nov 1 '07 #1
6 8581
deepuv04
227 Expert 100+
if @var is null
begin
select col1,col2... from table_name where EnterpriseId > 0
end
else
begin
select col1,col2... from table_name where EnterpriseId in @var
end
Nov 2 '07 #2
qhjghz
26
if @var is null
begin
select col1,col2... from table_name where EnterpriseId > 0
end
else
begin
select col1,col2... from table_name where EnterpriseId in @var
end
This is the best thing to do from performance point of view. But, however, if you can afford for a table scan then you can use this method

select * from
table_name a
where
CASE WHEN (@variable is null) THEN 1 ELSE
CASE WHEN a.column_name = @variable THEN 1 ELSE 0 END
END =1
Nov 3 '07 #3
sayedul
12
Hi Folks,

I have a parameter in a stored proc that can be null, a single value or a list of values delimited by commas, here's my problem

If the value is null I want the field to be any value but if it's not I want it to be in the list, how to I put that in the where clause?

The logic is something like

if @var is null then
EnterpriseId > 0
else EnterpriseId in (@var)

Hi,

If data type of EnterpriseId is varchar/char, try with where condition like below:
Expand|Select|Wrap|Line Numbers
  1. where (','+isnull(@var,EnterpriseId)+',') like ('%,'+EnterpriseId+ ',%')
  2.  
If data type of EnterpriseId is any numeric, try with where condition like below:
Expand|Select|Wrap|Line Numbers
  1. where (','+isnull(@var,convert(varchar(20),EnterpriseId))+',') like ('%,'+convert(varchar(20),EnterpriseId)+ ',%')
  2.  
The above should be a perfect and simple solution of your problem.

Regards,
Sayedul Haque
Nov 6 '07 #4
qhjghz
26
Hi,

If data type of EnterpriseId is varchar/char, try with where condition like below:
Expand|Select|Wrap|Line Numbers
  1. where (','+isnull(@var,EnterpriseId)+',') like ('%,'+EnterpriseId+ ',%')
  2.  
If data type of EnterpriseId is any numeric, try with where condition like below:
Expand|Select|Wrap|Line Numbers
  1. where (','+isnull(@var,convert(varchar(20),EnterpriseId))+',') like ('%,'+convert(varchar(20),EnterpriseId)+ ',%')
  2.  
The above should be a perfect and simple solution of your problem.

Regards,
Sayedul Haque

The performance will be very poor for this sql.
Nov 6 '07 #5
sayedul
12
The performance will be very poor for this sql.
Hi,

To fulfill all the requirements of the problem, I am sure that the other solutions except mine will not work. I think the other members who gave solution did not understand the problem completely or did not test their script to solve all the cases of the problem. The problem expresses that the parameter can be any of three types i.e. null or single value or multiple values separated by comma.

I shall be pleased if any of you can make another alternative perfect and simple solution of the problem and upload it. One thing you should remember that whenever you make a solution, the solution should be complete first then the performance issue will arise.

Thanks.

- Sayedul
Nov 7 '07 #6
Hi,

I have had this same dilema and have used a variable to construct a statement and then execute it - here's an example that works for me. you will need to change the table name obviously. and ofcourse the @Pvar variable can be have multiple values, or a null.

Declare @PVar as Varchar(80)
declare @SqlStr as Varchar(8000)
Set @Pvar = '12141,12142,12143'
set @SqlStr = 'Select * from tblInvoice '

IF @PVar is not null
Set @SqlStr = @SqlStr + ' Where InvoiceNo in (' + @PVar + ')'

Print @SqlStr

exec (@SqlStr)


I hope that answers the question.

:)


Hi,

To fulfill all the requirements of the problem, I am sure that the other solutions except mine will not work. I think the other members who gave solution did not understand the problem completely or did not test their script to solve all the cases of the problem. The problem expresses that the parameter can be any of three types i.e. null or single value or multiple values separated by comma.

I shall be pleased if any of you can make another alternative perfect and simple solution of the problem and upload it. One thing you should remember that whenever you make a solution, the solution should be complete first then the performance issue will arise.

Thanks.

- Sayedul
Nov 7 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Brian Shannon | last post by:
I have 3 combo boxes and two date text boxes on a .aspx page. The user can fill in any of the 5 controls or none to filter a datagrid. I was hoping someone could explain how to efficiently build...
7
by: Britney | last post by:
Original code: this.oleDbSelectCommand1.CommandText = "SELECT TOP 100 user_id, password, nick_name, sex, age, has_picture, city, state, " + "country FROM dbo.users WHERE (has_picture = ?) AND (sex...
2
by: jaYPee | last post by:
I have no problem setting the selectcommand in sqldataadapter to fetch record from sqlserver w/ where clause in parent table. however, my problem is on how can i fetch the child table which is...
2
by: The Other Mike | last post by:
VS 2005 Is their a way to dynamically change the where clause in a datagridview or dataset? I have a search window where the user can enter a part number or description or customer etc... and...
0
by: Tobin | last post by:
I am new to ASP.NET 2.0 and am redesigning some web pages developed in classic ASP. A common task was for me to accept input of text boxes and date controls to provide a filtered list for the...
6
by: Velislav | last post by:
Hi, I have a client script block, which is registered in my Page_Load. However a button may result in the need to change the script which I've registered. Obviously the OnClick event occurs...
1
by: not_a_commie | last post by:
I was hoping for increased functionality with the where clause in C# 3.0. Using the new keyword 'var' would really allow us to take nice advantage of these. Specifically: 1. I want to limit it...
0
by: rkandas | last post by:
Order by based on input Select col1, col2, col3 from table 1 order by col1 <ASC/DESC>, col2 <ASC/DESC>, col3 <ASC/DESC> The sort order for col1, col2, col3 are parameters to the program. Can...
20
by: billmaclean1 | last post by:
I need to write a stored procedure that selects from a table and returns the result set. I don't always know the TableSchema that I need to use when qualifying the table at run-time Example:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.