<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Catch-all queries</title>
	<atom:link href="http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/feed/" rel="self" type="application/rss+xml" />
	<link>http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/</link>
	<description>A discussion on SQL Server</description>
	<lastBuildDate>Fri, 03 Sep 2010 13:39:12 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Daniel</title>
		<link>http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/comment-page-1/#comment-692</link>
		<dc:creator>Daniel</dc:creator>
		<pubDate>Wed, 01 Sep 2010 06:04:09 +0000</pubDate>
		<guid isPermaLink="false">http://sqlinthewild.co.za/?p=174#comment-692</guid>
		<description>I guess Walker&#039;s query logic can be modified as such to handle the scenario when two or three parameters are passed.
WHERE ((key1 = @key1 AND @key1 IS NOT NULL) OR (@key1 IS NULL))
AND ((key2 = @key2 AND @key2 IS NOT NULL) OR (@key2 IS NULL))
AND ((key3 = @key3 AND @key3 IS NOT NULL) OR (@key3 IS NULL))

btw, this logic would be a better approach than the dynamic query..atleast for these types of situatios...</description>
		<content:encoded><![CDATA[<p>I guess Walker&#8217;s query logic can be modified as such to handle the scenario when two or three parameters are passed.<br />
WHERE ((key1 = @key1 AND @key1 IS NOT NULL) OR (@key1 IS NULL))<br />
AND ((key2 = @key2 AND @key2 IS NOT NULL) OR (@key2 IS NULL))<br />
AND ((key3 = @key3 AND @key3 IS NOT NULL) OR (@key3 IS NULL))</p>
<p>btw, this logic would be a better approach than the dynamic query..atleast for these types of situatios&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Emtucifor</title>
		<link>http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/comment-page-1/#comment-357</link>
		<dc:creator>Emtucifor</dc:creator>
		<pubDate>Mon, 19 Apr 2010 18:14:59 +0000</pubDate>
		<guid isPermaLink="false">http://sqlinthewild.co.za/?p=174#comment-357</guid>
		<description>David Walker, your query isn&#039;t logically the same. If 3 keys were passed in, your OR query would pull all rows where any one matches, not rows where all three match.</description>
		<content:encoded><![CDATA[<p>David Walker, your query isn&#8217;t logically the same. If 3 keys were passed in, your OR query would pull all rows where any one matches, not rows where all three match.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gail</title>
		<link>http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/comment-page-1/#comment-356</link>
		<dc:creator>Gail</dc:creator>
		<pubDate>Fri, 13 Nov 2009 09:18:43 +0000</pubDate>
		<guid isPermaLink="false">http://sqlinthewild.co.za/?p=174#comment-356</guid>
		<description>Main drawbacks of dynamic is security. Also the loss of syntax highlighting, compile time error checking and intellisense. It&#039;s not something I&#039;d use everywhere, just for the places it does work well (and this is one of them)</description>
		<content:encoded><![CDATA[<p>Main drawbacks of dynamic is security. Also the loss of syntax highlighting, compile time error checking and intellisense. It&#8217;s not something I&#8217;d use everywhere, just for the places it does work well (and this is one of them)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael</title>
		<link>http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/comment-page-1/#comment-355</link>
		<dc:creator>Michael</dc:creator>
		<pubDate>Thu, 12 Nov 2009 20:20:19 +0000</pubDate>
		<guid isPermaLink="false">http://sqlinthewild.co.za/?p=174#comment-355</guid>
		<description>I thought that dynamic SQL was a worse choice than the catch-all option, but this article says otherwise ?  I got a big chunk of dynamic sql from a contractor and it&#039;s harder to follow than non-dynamic. What are the drawbacks of dynamic ?</description>
		<content:encoded><![CDATA[<p>I thought that dynamic SQL was a worse choice than the catch-all option, but this article says otherwise ?  I got a big chunk of dynamic sql from a contractor and it&#8217;s harder to follow than non-dynamic. What are the drawbacks of dynamic ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Walker</title>
		<link>http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/comment-page-1/#comment-354</link>
		<dc:creator>David Walker</dc:creator>
		<pubDate>Thu, 05 Nov 2009 16:40:56 +0000</pubDate>
		<guid isPermaLink="false">http://sqlinthewild.co.za/?p=174#comment-354</guid>
		<description>I think you left out an important section from &quot;Dynamic Search Conditions&quot;.  Instead of using this form:

WHERE (key1 = @key1 OR @key1 IS NULL)
  AND (key2 = @key2 OR @key2 IS NULL)
  AND (key3 = @key3 OR @key3 IS NULL

you should consider using this:

WHERE (key1 = @key1 AND @key1 IS NOT NULL)
   OR (key2 = @key2 AND @key2 IS NOT NULL)
   OR (key3 = @key3 AND @key3 IS NOT NULL)

As Erland Sommarskog says, &quot;The odds are very good that SQL Server will generate a plan which seeks the three key indexes and then merges the result either by index concatenation or some other method. Here is the real big scoop: thanks to the condition, @x IS NOT NULL, SQL Server adds a filter with a startup expression to the plan, so if the corresponding variable is NULL, SQL Server will not access that index at all.&quot;

I have found in my testing that this solution performs very well; indexes ARE used, performance is great, and dynamic code is not required.</description>
		<content:encoded><![CDATA[<p>I think you left out an important section from &#8220;Dynamic Search Conditions&#8221;.  Instead of using this form:</p>
<p>WHERE (key1 = @key1 OR @key1 IS NULL)<br />
  AND (key2 = @key2 OR @key2 IS NULL)<br />
  AND (key3 = @key3 OR @key3 IS NULL</p>
<p>you should consider using this:</p>
<p>WHERE (key1 = @key1 AND @key1 IS NOT NULL)<br />
   OR (key2 = @key2 AND @key2 IS NOT NULL)<br />
   OR (key3 = @key3 AND @key3 IS NOT NULL)</p>
<p>As Erland Sommarskog says, &#8220;The odds are very good that SQL Server will generate a plan which seeks the three key indexes and then merges the result either by index concatenation or some other method. Here is the real big scoop: thanks to the condition, @x IS NOT NULL, SQL Server adds a filter with a startup expression to the plan, so if the corresponding variable is NULL, SQL Server will not access that index at all.&#8221;</p>
<p>I have found in my testing that this solution performs very well; indexes ARE used, performance is great, and dynamic code is not required.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian</title>
		<link>http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/comment-page-1/#comment-353</link>
		<dc:creator>Brian</dc:creator>
		<pubDate>Thu, 24 Sep 2009 23:17:19 +0000</pubDate>
		<guid isPermaLink="false">http://sqlinthewild.co.za/?p=174#comment-353</guid>
		<description>Thank you so much for this article, it was very informative and extremely helpful.   This is by far one of the most useful SQL articles I&#039;ve read over the years!</description>
		<content:encoded><![CDATA[<p>Thank you so much for this article, it was very informative and extremely helpful.   This is by far one of the most useful SQL articles I&#8217;ve read over the years!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gail</title>
		<link>http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/comment-page-1/#comment-352</link>
		<dc:creator>Gail</dc:creator>
		<pubDate>Mon, 21 Sep 2009 19:35:10 +0000</pubDate>
		<guid isPermaLink="false">http://sqlinthewild.co.za/?p=174#comment-352</guid>
		<description>I think that may depend on the server collation. AdventureWorks is case insensitive on my server (as are all of my DBs). After a rather nasty experience a few years back I&#039;m allergic to case-sensitive databases ;-)</description>
		<content:encoded><![CDATA[<p>I think that may depend on the server collation. AdventureWorks is case insensitive on my server (as are all of my DBs). After a rather nasty experience a few years back I&#8217;m allergic to case-sensitive databases <img src='http://sqlinthewild.co.za/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ed</title>
		<link>http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/comment-page-1/#comment-351</link>
		<dc:creator>Ed</dc:creator>
		<pubDate>Mon, 21 Sep 2009 17:45:00 +0000</pubDate>
		<guid isPermaLink="false">http://sqlinthewild.co.za/?p=174#comment-351</guid>
		<description>Gail - Nevermind I  figured out my problem.  The sample AdventureWorks DB is set to a case senative collation.</description>
		<content:encoded><![CDATA[<p>Gail &#8211; Nevermind I  figured out my problem.  The sample AdventureWorks DB is set to a case senative collation.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ed</title>
		<link>http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/comment-page-1/#comment-350</link>
		<dc:creator>Ed</dc:creator>
		<pubDate>Mon, 21 Sep 2009 17:35:05 +0000</pubDate>
		<guid isPermaLink="false">http://sqlinthewild.co.za/?p=174#comment-350</guid>
		<description>Gail,  I must be missing something because I&#039;ve tinkered with yoru example and no matter what I try I get errors.  I repalced all the charatcers that did not copy over correctly like the single quotes but still it errors out on me.</description>
		<content:encoded><![CDATA[<p>Gail,  I must be missing something because I&#8217;ve tinkered with yoru example and no matter what I try I get errors.  I repalced all the charatcers that did not copy over correctly like the single quotes but still it errors out on me.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Catch all queries and indexing - SQL and the like</title>
		<link>http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/comment-page-1/#comment-349</link>
		<dc:creator>Catch all queries and indexing - SQL and the like</dc:creator>
		<pubDate>Tue, 08 Sep 2009 09:59:47 +0000</pubDate>
		<guid isPermaLink="false">http://sqlinthewild.co.za/?p=174#comment-349</guid>
		<description>[...] many others, Gail Shaw has blogged on the subject of catch all queries.&#160; On many occasions I have needed to do something similar and found performance to be pretty [...]</description>
		<content:encoded><![CDATA[<p>[...] many others, Gail Shaw has blogged on the subject of catch all queries.&nbsp; On many occasions I have needed to do something similar and found performance to be pretty [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>
