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.
| Author |
Topic |
|
kaepten
Starting Member
2 Posts |
Posted - 2006-06-27 : 07:36:48
|
| I know it's unbelievable, but i didn't ever use SP before. Sorry for this noobish question, don't beat me for this - please ;-)In my SELECT Query i like to have some (WHERE) paramteters le's say: ID, NAME and AGE. I allways programmicali generate a WHERE statement in relation to which parameters I really get, because not allwasy i get all the parameters together. This is fine for "normal" Tables.How does this work with SP? If I don't get NAME, do I set the SP-Parameter NAME to null to unconsider it? Or do I have to make some IF statements in the SP to check, if NAME has some value?Thanks for a short hint! |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-06-27 : 08:11:46
|
You could set Name to NULL and then check for which parameters in SP that are non-null. Or use COALESCE function or ISNULL function.IN:@ID 25@Name NULL@Age 38SELECT * FROM WhatEverTableWHERE ID = ISNULL(@ID, ID) AND Name = ISNULL(@Name, Name) AND Age = ISNULL(@Age, Age) Peter LarssonHelsingborg, Sweden |
 |
|
|
kaepten
Starting Member
2 Posts |
Posted - 2006-06-27 : 08:40:45
|
| Thank you very much!This works fine in my test application. Is this the usual way to work with various parameters? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-06-27 : 08:49:05
|
orSELECT * FROM WhatEverTableWHERE (ID = @ID or @ID is null) AND (Name =@Name or @Name is null) AND (Age =@Age or @Age is null) MadhivananFailing to plan is Planning to fail |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-06-27 : 09:09:57
|
quote: Originally posted by kaepten Thank you very much!This works fine in my test application. Is this the usual way to work with various parameters?
More or less, yes. The choice of approach is only the habit of the programmer.Peter LarssonHelsingborg, Sweden |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-27 : 10:54:49
|
quote: The choice of approach is only the habit of the programmer.
According to the 'gospel' article on this (http://www.sommarskog.se/dyn-search.html), it's not just habit since performance can be affected. That article suggests that the following approach can result in a table scan being avoided (with the right indexes, of course):SELECT * FROM WhatEverTableWHERE (ID = @ID and @ID is not null) AND (Name = @Name and @Name is not null) AND (Age = @Age and @Age is not null) Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-06-27 : 11:39:41
|
According to Mr Erland Sommarskog himselfquote: I ran the six test searches in Northgale. On SQL 2005, search_orders_3a ran considerably slower than search_orders_3, whereas on SQL 2000, it [COALESCE] was considerably faster!
Is anyone up for a speed comparison on a million row table?Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|
|