<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.technet.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Database Programming: One Last Gasp On Optional Parameters</title><link>http://blogs.technet.com/wardpond/archive/2006/07/10/database-programming-one-last-gasp-on-optional-parameters.aspx</link><description>July 12 update: Everything that follows is WRONG . See Adam's comment and my follow-up . You might think I'm obsessing over this, and you may well be right, but I wanted to make one more comment about handling optional parameters. The issue with the syntax</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Database Programming: One Last Gasp On Optional Parameters</title><link>http://blogs.technet.com/wardpond/archive/2006/07/10/database-programming-one-last-gasp-on-optional-parameters.aspx#441390</link><pubDate>Thu, 13 Jul 2006 06:05:56 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:441390</guid><dc:creator>Adam Machanic</dc:creator><description>I hope you're joking, Ward?&lt;br&gt;&lt;br&gt;---&lt;br&gt;use tempdb&lt;br&gt;go&lt;br&gt;&lt;br&gt;select&lt;br&gt;	y1.number as y1,&lt;br&gt;	y2.number as y2&lt;br&gt;into wardstable&lt;br&gt;from &lt;br&gt;	master..spt_values y1,&lt;br&gt;	master..spt_values y2&lt;br&gt;where&lt;br&gt;	y1.type = 'P' and y1.number &amp;lt;= 500&lt;br&gt;	and y2.type = 'P' and y2.number &amp;lt;= 500&lt;br&gt;go&lt;br&gt;&lt;br&gt;create clustered index y1 on wardstable (y1)&lt;br&gt;create nonclustered index y2 on wardstable (y2)&lt;br&gt;go&lt;br&gt;&lt;br&gt;create procedure GetWardsNumber&lt;br&gt;	@y1 int = null,&lt;br&gt;	@y2 int = null&lt;br&gt;as&lt;br&gt;begin&lt;br&gt;	set nocount on&lt;br&gt;&lt;br&gt;	select *&lt;br&gt;	from wardstable&lt;br&gt;	where&lt;br&gt;		y1 = coalesce(@y1, y1)&lt;br&gt;		and y2 = coalesce(@y2, y2)&lt;br&gt;end&lt;br&gt;go&lt;br&gt;&lt;br&gt;create procedure GetAdamsNumber&lt;br&gt;	@y1 int = null,&lt;br&gt;	@y2 int = null&lt;br&gt;as&lt;br&gt;begin&lt;br&gt;	set nocount on&lt;br&gt;&lt;br&gt;	declare @sql nvarchar(max)&lt;br&gt;	set @sql = &lt;br&gt;		'select * ' +&lt;br&gt;		'from wardstable ' +&lt;br&gt;		'where 1=1 ' +&lt;br&gt;			case&lt;br&gt;				when @y1 is not null then&lt;br&gt;					'and y1 = @y1 '&lt;br&gt;				else ''&lt;br&gt;			end +&lt;br&gt;			case&lt;br&gt;				when @y2 is not null then&lt;br&gt;					'and y2 = @y2 '&lt;br&gt;				else ''&lt;br&gt;			end&lt;br&gt;&lt;br&gt;	exec sp_executesql&lt;br&gt;		@sql,&lt;br&gt;		N'@y1 int, @y2 int',&lt;br&gt;		@y1,&lt;br&gt;		@y2&lt;br&gt;end&lt;br&gt;go&lt;br&gt;&lt;br&gt;&lt;br&gt;set statistics io on&lt;br&gt;go&lt;br&gt;&lt;br&gt;exec getwardsnumber @y1 = 1&lt;br&gt;go&lt;br&gt;&lt;br&gt;exec getwardsnumber @y2 = 2&lt;br&gt;go&lt;br&gt;&lt;br&gt;exec getadamsnumber @y1 = 1&lt;br&gt;go&lt;br&gt;&lt;br&gt;exec getadamsnumber @y2 = 2&lt;br&gt;go&lt;br&gt;&lt;br&gt;set statistics io off&lt;br&gt;go&lt;br&gt;&lt;br&gt;drop proc getadamsnumber&lt;br&gt;go&lt;br&gt;drop proc getwardsnumber&lt;br&gt;go&lt;br&gt;drop table wardstable&lt;br&gt;go&lt;br&gt;---</description></item><item><title>re: Database Programming: One Last Gasp On Optional Parameters</title><link>http://blogs.technet.com/wardpond/archive/2006/07/10/database-programming-one-last-gasp-on-optional-parameters.aspx#441394</link><pubDate>Thu, 13 Jul 2006 06:31:00 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:441394</guid><dc:creator>Ward Pond</dc:creator><description>Boy, Adam, I can sure see why you ask the question.&lt;br&gt;&lt;br&gt;Readers, Adam's syntax in the sample generates 6 logical reads while mine generates 594.&lt;br&gt;&lt;br&gt;I hereby renounce all forms of the COALESCE syntax, and furthermore promise not to write any more performance-related blog posts while I'm packing for a vacation.&lt;br&gt;&lt;br&gt;Thanks, Adam, for setting me straight.</description></item><item><title>Ward Pond's SQL Server blog : Optional Parameters: Adam Hopes I'm Joking, But The Joke's On Me</title><link>http://blogs.technet.com/wardpond/archive/2006/07/10/database-programming-one-last-gasp-on-optional-parameters.aspx#441396</link><pubDate>Thu, 13 Jul 2006 06:37:24 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:441396</guid><dc:creator>Ward Pond's SQL Server blog : Optional Parameters: Adam Hopes I'm Joking, But The Joke's On Me</dc:creator><description>PingBack from &lt;a rel="nofollow" target="_new" href="http://blogs.technet.com/wardpond/archive/2006/07/12/441395.aspx"&gt;http://blogs.technet.com/wardpond/archive/2006/07/12/441395.aspx&lt;/a&gt;</description></item><item><title>Ward Pond's SQL Server blog : Database Programming: One Last Gasp On Optional Parameters</title><link>http://blogs.technet.com/wardpond/archive/2006/07/10/database-programming-one-last-gasp-on-optional-parameters.aspx#441398</link><pubDate>Thu, 13 Jul 2006 06:40:29 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:441398</guid><dc:creator>Ward Pond's SQL Server blog : Database Programming: One Last Gasp On Optional Parameters</dc:creator><description>PingBack from &lt;a rel="nofollow" target="_new" href="http://blogs.technet.com/wardpond/archive/2006/07/10/441050.aspx"&gt;http://blogs.technet.com/wardpond/archive/2006/07/10/441050.aspx&lt;/a&gt;</description></item><item><title>Pond’s Eleventh Law Emerges From Reflection Upon The Perils Of Going Fast</title><link>http://blogs.technet.com/wardpond/archive/2006/07/10/database-programming-one-last-gasp-on-optional-parameters.aspx#442742</link><pubDate>Fri, 21 Jul 2006 10:33:46 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:442742</guid><dc:creator>Ward Pond's SQL Server blog</dc:creator><description>When I first published Pond’s Laws, I promised it would be a living document. &amp;nbsp;Herewith is the first evidence.</description></item><item><title>Everything about dynamic search conditions in T-SQL</title><link>http://blogs.technet.com/wardpond/archive/2006/07/10/database-programming-one-last-gasp-on-optional-parameters.aspx#2809097</link><pubDate>Fri, 01 Feb 2008 13:23:13 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:2809097</guid><dc:creator>dave^2=-1</dc:creator><description>&lt;p&gt;There are several common methods for using dynamic search conditions (a.k.a. optional parameters) in&lt;/p&gt;
</description></item></channel></rss>