<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>High Tech Redneck Woman &#187; mssql</title>
	<atom:link href="http://www.hightechredneckwoman.com/tag/mssql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hightechredneckwoman.com</link>
	<description>I&#039;m a Redneck Woman. I&#039;m a High Tech Broad.</description>
	<lastBuildDate>Sun, 06 Nov 2011 19:51:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Optional should be OPTIONAL</title>
		<link>http://www.hightechredneckwoman.com/2008/05/23/optional-should-be-optional/</link>
		<comments>http://www.hightechredneckwoman.com/2008/05/23/optional-should-be-optional/#comments</comments>
		<pubDate>Fri, 23 May 2008 22:48:39 +0000</pubDate>
		<dc:creator>Becky</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[mssql]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.hightechredneckwoman.com/?p=271</guid>
		<description><![CDATA[<p>Yesterday and today, I've been working on some changes to our discount system (at work).  The majority of the changes dealt with the (mssql) database ... stored procedures and user defined functions (ufn).  One of the changes was to get the shopping cart subtotal based on different criteria ... </p>
<ul>
<li>If the shopping cart had an order-level discount in it, and that discount was set up to override any discount exclusions that were set up, the </li>&#160;[&#8230;]</ul>]]></description>
			<content:encoded><![CDATA[<p>Yesterday and today, I've been working on some changes to our discount system (at work).  The majority of the changes dealt with the (mssql) database ... stored procedures and user defined functions (ufn).  One of the changes was to get the shopping cart subtotal based on different criteria ... </p>
<ul>
<li>If the shopping cart had an order-level discount in it, and that discount was set up to override any discount exclusions that were set up, the subtotal calculation needed to only exclude those products where the exclusion wasn't allowed to be overridden.</li>
<li>If the shopping cart had an order-level discount in it, and that discount was NOT set up to override any discount exclusions that were set up, the subtotal calculation needed to exclude ALL products that had exclusions set up.</li>
</ul>
<p>We have a ufn set up to calculate the order subtotal.  This ufn is already taking into account the discount exclusions.  But with my changes, I needed it to now look at the exclusion overrides.  I was hoping that ufn's allowed for optional parameters like php functions do.  So I set up a little test to see ...</p>
<div class="igBar"><span id="lsql-4"><a href="#" onclick="javascript:showPlainTxt('sql-4'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-4">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> <span style="color:#006600; font-weight:bold;">&#91;</span>dbo<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#006600; font-weight:bold;">&#91;</span>ufn_MyTest<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#40;</span>@Param1 int, @Param2 int = <span style="color: #993333; font-weight: bold;">NULL</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">RETURNS varchar<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #cc66cc;color:#800000;">500</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">AS</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">BEGIN</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; DECLARE @MyVariable varchar<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #cc66cc;color:#800000;">500</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">IF</span> @Param1&gt; <span style="color: #cc66cc;color:#800000;">10</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">SET</span> @MyVariable = <span style="color: #ff0000;">'Incoming parameter #1 is greater than 10.'</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; ELSE</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">SET</span> @MyVariable = <span style="color: #ff0000;">'Incoming parameter #1 is less than 10.'</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">IF</span> @Param2 <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; BEGIN</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">IF</span> @Param2&gt; <span style="color: #cc66cc;color:#800000;">10</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">SET</span> @MyVariable = @MyVariable + <span style="color: #ff0000;">' Incoming parameter #2 is greater than 10.'</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ELSE</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">SET</span> @MyVariable = @MyVariable + <span style="color: #ff0000;">' Incoming parameter #2 is less than 10.'</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; END</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">RETURN</span> @MyVariable</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">END </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The database let me create the function, so I was hopeful that this was gonna work.  So I tried executing my little ufn ...</p>
<div class="igBar"><span id="lsql-5"><a href="#" onclick="javascript:showPlainTxt('sql-5'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-5">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">SELECT</span> dbo.ufn_MyTest<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #cc66cc;color:#800000;">8</span><span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>But that didn't work.  It gave me an error ...</p>
<p><code>An insufficient number of arguments were supplied for the procedure or function dbo.ufn_myTest.</code></p>
<p>What?  Come on.  That second parameter is set up to be optional.  So I did some searching and found this tidbit ...</p>
<blockquote><p>from the MSSQL manual<br />
When a parameter of the function has a default value, the keyword "default" must be specified when calling the function in order to get the default value. This behavior is different from parameters with default values in stored procedures in which omitting the parameter also implies the default value.</p></blockquote>
<p>Grrr!!  <img src='http://www.hightechredneckwoman.com/wp-includes/images/smilies/banghead.gif' alt=':banghead:' class='wp-smiley' /> So I tried this ...</p>
<div class="igBar"><span id="lsql-6"><a href="#" onclick="javascript:showPlainTxt('sql-6'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-6">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">SELECT</span> dbo.ufn_MyTest<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #cc66cc;color:#800000;">8</span>, <span style="color: #993333; font-weight: bold;">DEFAULT</span><span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>THAT worked.  But doesn't that kind of defeat the purpose of having an "optional" parameter.  When I think "optional", I see that as meaning that the parameter doesn't have to be passed or it can be, depending on the situation.  If I have to put 'DEFAULT' in it's place, that doesn't make it optional.  I could just as well pass in the "optional" value.  </p>
<p>To fix my little conundrum, I created a separate ufn and called the appropriate one based on the parameter that I had wanted to pass in as an optional one.  That worked like a charm.  I just wish that Microsoft would have set up mssql ufn's to accept truly optional parameters.  But I guess I'm not surprised.  It is Microsoft after all.   <img src='http://www.hightechredneckwoman.com/wp-includes/images/smilies/neener.gif' alt=':nahnah:' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.hightechredneckwoman.com/2008/05/23/optional-should-be-optional/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

