Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 multi paramaters agains one table

Author  Topic 

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2011-04-07 : 12:13:20
hi,

this is a sample based on adventureworks database:

use adventureworks;
go

declare @firstname nvarchar(50)
declare @lastname nvarchar(50)
declare @phone nvarchar(25)
declare @ID nvarchar(20)

--set @ID = 31
--set @lastname = 'Al'
--set @phone = '47'

select
contactID
,firstname
,lastname
,emailaddress
,phone
from Person.Contact

where
-- contactid like isnull(@ID, '%')
and firstname like isnull(@firstname, '%')
and lastname like isnull(@lastname, '%')
and phone like isnull(@phone,'%')


I have four parameters (@ID, @firstname, @lastname, @phone) and i have have either one (random) parameter set or two or all or non. in each case i want to have all corresponding rows returned.

for example if i set parameter @lastname to 'Alv' i want to have returned results same as with query:

select * from person.contact where lastname like 'alv%'

same goes for all the other parameters.

only @ID must have exact match -without like operater.

is there a better practise to do this.

thank you

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-04-07 : 12:23:49
[code]
where
(@ID is null or contactid = @ID)
and (@firstName is null or firstname like isnull(@firstname, '%'))
and (@lastName is null or lastname like isnull(@lastname, '%'))
and (@phone is null or phone like isnull(@phone,'%'))
[/code]



Corey

I Has Returned!!
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-04-07 : 12:25:24
Many people, including SQL Teams very own Gail Shaw have written about this. Here are a few links:


http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/
http://blogs.msdn.com/b/bartd/archive/2009/05/03/sometimes-the-simplest-solution-isn-t-the-best-solution-the-all-in-one-search-query.aspx
http://www.sommarskog.se/dyn-search-2005.html
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-04-07 : 12:25:58
Corey! Again -->

Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2011-04-07 : 12:33:08
thank you for answers, but still no useful solutions.

As I said, I want to use like operator e.g.: firstName like @firstname instead of full match: firstname = @firstname.
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-04-07 : 12:34:20
...

Although, I should add that in the grand scheme of things, too many (null or satisified) conditions tend to make it difficult for the query to store a decent (appropriate) query plan for the various combinations of significant parameter values.

For example, if people generally search using the @id param... and search by @lastName will not be as fast as it could be because the cached plan had @id in mind...

not sure what the links say... but maybe they mention this -^

Corey

I Has Returned!!
Go to Top of Page
   

- Advertisement -