Following on from my last question Sql Server query performance, and discovering that my method of allowing optional parameters in a search query is sub optimal, does anyone
Ok, here we go
OPTION (RECOMPILE)
is a must - otherwise the first query plan is being reused, regardless how the parameters match. Sorry, no real way to do that better.
Besides that - no, sorry. Dynamic SQL can get more efficient (by avoiding the IS NULL alternatives), but you basically have it nailed down if that is not possible.
With Dynamic SQL you basically do not have aline for HP.phonenumber if the HomePhone variable is null ;)