Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Search Query Expand / Collapse
Author
Message
Posted Monday, November 16, 2009 11:37 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, May 3, 2012 12:17 AM
Points: 97, Visits: 1,586
Hi,
I have to write a query for searching records.
I have four input fields.
If USER is entering value in one
input field data to be serached on that specific single field.
If more than one entry then all parameters should be ANDed.
I don't want to use if A='' Then Do this

Please suggest me any good idea.


Shatrughna
Post #819830
Posted Tuesday, November 17, 2009 12:14 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 9:59 AM
Points: 579, Visits: 1,806
'Catch all' ,follow the link

http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/


---------------------------------------------------------------------------------
Post #819841
Posted Tuesday, November 17, 2009 12:15 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, May 15, 2014 5:11 PM
Points: 6,067, Visits: 5,283
You could try this:
SELECT * 
FROM dbo.SomeTable
WHERE ( @Field1 IS NULL OR Field1 = @Field1 )
AND ( @Field2 IS NULL OR Field2 = @Field2 )
AND ( @Field3 IS NULL OR Field3 = @Field3 )
AND ( @Field4 IS NULL OR Field4 = @Field4 )

Provided that the search fields are NULL when unused and non-NULL when used.

CEWII

Post #819842
Posted Tuesday, November 17, 2009 12:31 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, May 3, 2012 12:17 AM
Points: 97, Visits: 1,586
Thanks.
Appreciated.


Shatrughna
Post #819849
Posted Tuesday, November 17, 2009 1:19 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 3:14 AM
Points: 42,460, Visits: 35,520
Elliott W (11/17/2009)
You could try this:
SELECT * 
FROM dbo.SomeTable
WHERE ( @Field1 IS NULL OR Field1 = @Field1 )
AND ( @Field2 IS NULL OR Field2 = @Field2 )
AND ( @Field3 IS NULL OR Field3 = @Field3 )
AND ( @Field4 IS NULL OR Field4 = @Field4 )

Provided that the search fields are NULL when unused and non-NULL when used.


You could, providing that performance is not a major concern. Take a look at the link that Nabha posted.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #819867
Posted Tuesday, November 17, 2009 7:08 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, May 15, 2014 5:11 PM
Points: 6,067, Visits: 5,283
G,
I got the parameter sniffing vibe off that article.. I have always had really good performance off these kinds of queries because I have avoided the causes of that. As far as performance the where clause only gets evaluated once. The optimizer knows that if the variable is NULL that is already has a positive result so it doesn't even have to use that in the plan..

I tend to think this is better than dynamic SQL.. But I may do some additional testing..

CEWII
Post #820032
Posted Tuesday, November 17, 2009 7:17 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 3:14 AM
Points: 42,460, Visits: 35,520
Elliott W (11/17/2009)
I got the parameter sniffing vibe off that article..


It's not so much parameter sniffing. It's that there's no single optimal execution plan for that kind of query and, because the optimiser has to pick a safe plan, it generally picks a sub-optimal one.

The optimizer knows that if the variable is NULL that is already has a positive result so it doesn't even have to use that in the plan..

Not really. Optimiser doesn't deal well with multiple '@variable is null OR' constructs. Plus it can't (except in 2008 when OPTION RECOMPILE is specified) not use a particular branch in the plan when a parameter is null, because that plan will be cached for reuse and next time the query runs the parameter may not be null. If it made that assumption, the plan would result in incorrect results sometimes. That's not allowed, the plan must always be safe for reuse.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #820040
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse