November 16, 2009 at 11:37 pm
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
November 17, 2009 at 12:14 am
'Catch all' ,follow the link
http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/
---------------------------------------------------------------------------------
November 17, 2009 at 12:15 am
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
November 17, 2009 at 12:31 am
Thanks.
Appreciated.
Shatrughna
November 17, 2009 at 1:19 am
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, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
November 17, 2009 at 7:08 am
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
November 17, 2009 at 7:17 am
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, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply