May 08
Fri
23

Optional should be OPTIONAL

Posted at 5:48 pm in Development

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 ...

  • 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.
  • 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.

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 ...

SQL:
  1. CREATE FUNCTION [dbo].[ufn_MyTest](@Param1 int, @Param2 int = NULL)
  2. RETURNS varchar(500)
  3. AS
  4. BEGIN
  5.     DECLARE @MyVariable varchar(500)
  6.  
  7.     IF @Param1> 10
  8.         SET @MyVariable = 'Incoming parameter #1 is greater than 10.'
  9.     ELSE
  10.         SET @MyVariable = 'Incoming parameter #1 is less than 10.'
  11.  
  12.     IF @Param2 IS NOT NULL
  13.         BEGIN
  14.             IF @Param2> 10
  15.                 SET @MyVariable = @MyVariable + ' Incoming parameter #2 is greater than 10.'
  16.             ELSE
  17.                 SET @MyVariable = @MyVariable + ' Incoming parameter #2 is less than 10.'
  18.         END
  19.  
  20.     RETURN @MyVariable
  21. END

The database let me create the function, so I was hopeful that this was gonna work. So I tried executing my little ufn ...

SQL:
  1. SELECT dbo.ufn_MyTest(8)

But that didn't work. It gave me an error ...

An insufficient number of arguments were supplied for the procedure or function dbo.ufn_myTest.

What? Come on. That second parameter is set up to be optional. So I did some searching and found this tidbit ...

from the MSSQL manual
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.

Grrr!! :banghead: So I tried this ...

SQL:
  1. SELECT dbo.ufn_MyTest(8, DEFAULT)

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.

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. :nahnah:

Comments are closed.

I'm a Redneck Woman
Archives

Search
 
I'm a High Tech Broad
Stats
492 Posts & 80 Comments
Since 07 January 2007

48 queries in 0.892 seconds.
Feeds
Advertising
Meta
BlogRoll
Other Stuff
50% High Tech  50% Redneck
100% Woman
Valid CSS  Valid XHTML
This site looks best in Firefox  This site looks best at 1024x768 or higher
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
Where You Can Find Me
twitter   flickr   delicious   brightkite   facebook   friendfeed
infield parking   myspace   jaiku   pownce   dopplr
Twitter
  • There. My salad is all ready to go for lunch tomorrow. Only worried about how the cucs will hold up. More time tonite to make it up. 7 hrs ago
  • I'd better grill up some chicken so I have a tasty salad for lunch tomorrow. 8 hrs ago
  • Sucky part of working out when I get home from work: I don't get to eat supper 'til almost 7pm. 10 hrs ago
  • More updates...

Powered by Twitter Tools.

TwitterCounter for @hightechredneck
Affiliates
HostGator.com Affilliate

Get Smart. Get FireFox!

Get Thunderbird!
Other Cool Sites
kirtsy!
Tag Cloud
Recent TwitPic Uploads