Author |
Topic |
AskSQLTeam
Ask SQLTeam Question
0 Posts |
|
xianve
Starting Member
3 Posts |
Posted - 2007-10-11 : 17:33:31
|
What is bad about:-- Using OR @Parm IS NULLDECLARE @SalesOrderID INTSET @SalesOrderID = 43671 SELECT *FROM Sales.SalesOrderHeaderWHERE SalesOrderID = @SalesOrderID OR @SalesOrderID IS NULLI understand that dynamic sql is better probably, but dynamic sql is a pain to work with.I would re-write the query like this:SELECT *FROM Sales.SalesOrderHeaderWHERE (@SalesOrderID IS NULL OR (@SalesOrderID IS NOT NULL AND SalesOrderID = @SalesOrderID))That way the NULL is checked first and if true, there is no WHERE executed. |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2007-10-11 : 18:02:47
|
the example on nullable types is a bit off the mark: string? has no point because strings are reference types - all reference types may be null, so there's no no reason to wrap them in a Nullable<T>.it's more interesting for value types, such as int, short, long, structs, etc. That is, you can't write this:int i = null;but you can do this:int? i = null; elsasoft.org |
|
|
graz
Chief SQLTeam Crack Dealer
4149 Posts |
Posted - 2007-10-11 : 18:18:43
|
quote: Originally posted by xianve What is bad about:-- Using OR @Parm IS NULLDECLARE @SalesOrderID INTSET @SalesOrderID = 43671 SELECT *FROM Sales.SalesOrderHeaderWHERE SalesOrderID = @SalesOrderID OR @SalesOrderID IS NULLI understand that dynamic sql is better probably, but dynamic sql is a pain to work with.I would re-write the query like this:SELECT *FROM Sales.SalesOrderHeaderWHERE (@SalesOrderID IS NULL OR (@SalesOrderID IS NOT NULL AND SalesOrderID = @SalesOrderID))That way the NULL is checked first and if true, there is no WHERE executed.
Those examples won't use the index on the column. You'll get the correct result but it will take a table scan to get it.=================================================Creating tomorrow's legacy systems today. One crisis at a time. |
|
|
graz
Chief SQLTeam Crack Dealer
4149 Posts |
Posted - 2007-10-11 : 18:19:01
|
quote: Originally posted by jezemine the example on nullable types is a bit off the mark: string? has no point because strings are reference types - all reference types may be null, so there's no no reason to wrap them in a Nullable<T>.it's more interesting for value types, such as int, short, long, structs, etc. That is, you can't write this:int i = null;but you can do this:int? i = null; elsasoft.org
Yep, should have done a better job there.=================================================Creating tomorrow's legacy systems today. One crisis at a time. |
|
|
xianve
Starting Member
3 Posts |
Posted - 2007-10-11 : 18:29:23
|
quote: Originally posted by grazThose examples won't use the index on the column. You'll get the correct result but it will take a table scan to get it.=================================================Creating tomorrow's legacy systems today. One crisis at a time.
So the only way to do it is to have dynamic SQL? Is there no other way to control dynamic what where clauses to execute? |
|
|
graz
Chief SQLTeam Crack Dealer
4149 Posts |
Posted - 2007-10-12 : 08:35:15
|
I've been searching for a better solution than dynamic SQL. I haven't found one yet.=================================================Creating tomorrow's legacy systems today. One crisis at a time. |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-10-12 : 08:44:55
|
@xianve:order of equal importance conditions in the where clause is irrelevant._______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenpSSMS Add-in that does a few things: www.ssmstoolspack.com |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
|