Default Values

The Basics

In VB I just love optional parameters, it's a shame that C# doesn't have that feature(instead of overloading methods of different parameters.) In T-Sql we can simulate this optional parameters in our stored procedures by using default values:
PROC dbo.pr_GetInvoiceList (@PeriodMonth INT = 1, @PeriodYear INT = 2005)
AS
SELECT *
FROM InvoiceTable
WHERE PeriodMonth = @PeriodMonth AND PeriodYear =@PeriodYear


Then you can just execute your stored procedure sans the parameters:
EXEC pr_GetInvoiceList
The purpose of the default parameter values is to make sure our stored procedure will run even when there is no parameter assigned. Having said that however, the only time I find it is useful, is during testing.

An interesting application

I have used stored procs extensibly in my application, but I was kind of frustrated when a situation arises that not all parameters will be used for either an INSERT or UPDATE procedure. I use only a single stored proc to update for example any modification to our MedicalReports table. An update might contain just updating the last modified column or update both last modified column and approvedby column. I have to resort to client side updates to handle both scenario and find it cumbersome to maintain. Then I was toying the idea of default values, but instead of having actual values, I would instead default all the values to NULL.
PROC dbo.pr_UpdateMedRep(@RepId INT, @LastModBy VARCHAR(10)=NULL, @ApprovedBy VARCHAR(10)=NULL)

The body of our stored proc will be like this:

UPDATE MedicalReportsTable SET
LastModBy = ISNULL(@LastModBy, LastModBy),
ApprovedBy = ISNULL(@ApprovedBy, ApprovedBy)
WHERE MedicalReportsTable.RepId = @RepId


Now if I want to update only the LastModify column:
pr_UpDateMedRep 1, 'User1'

Updating only ApprovedBy column:
pr_UpDateMedRep 1, NULL, 'User2'

Without worrying that either column will be updated when it shouldn't be and I'm still using stored procedure to do so.

Published 02-16-2006 3:21 AM by bonskijr
Filed under:

Comments

# re: Default Values

Or, in case that you have the same stored procedure as this:

CREATE PROCEDURE SPR_SAMPLE_MISTERCLAY(
@LASTMOD AS SMALLDATETIME = '2/17/2006',
@LASTMODBY AS NVARCHAR(20) = 'Admin')
AS
BEGIN
--T-SQL STATEMENT/S HERE
END

You can disregard the assignment of the null value simply by indicating the variable name and the value when executing the stored procedure, just like this one:

EXEC SPR_SAMPLE_MISTERCLAY @LASTMODBY = 'User1'

In this case, you don't have to take note of the order of the parameters as aligned with their respective values and also, without assigning NULL value to the parameters that should take the defaulted value when neglected.

Thursday, February 16, 2006 9:06 AM by MisterClay

# re: Default Values

i still do overloading in VB! :P

I guess it's the 'CSharper' inside me that makes me use it all the time...

Thursday, February 16, 2006 9:30 AM by keithrull

# re: Default Values

I still prefer overloading in VB.NET

The major disadvantage of optional parameters is when sharing code with C# clients since the parameter is required by C# to utilize the method.

Thursday, February 16, 2006 6:58 PM by budoi

# re: Default Values

yeah i agree with you :) i think overloading is a great thing.. probably better than curly brackets! :P just kidding!

Thursday, February 16, 2006 7:54 PM by keithrull

# re: Default Values

Mr. Clay:

Thanks for pointing out the format on how to execute sp by indicating the parameter name. Actually I only use exec during testing, but in my source I do the same but using parameter object, just to be same from sql injection attacks. :)

Keith, Budoi:
You have a point there with regards to overloading, however I think it promotes code duplicates especially if the optional parameter has a minor impact on the method. :)

ps. do you just wish c# also has that capability? I know some C# developers wish it has the same functionality. :)

Thursday, February 16, 2006 9:59 PM by bonskijr

# re: Default Values

Yes, if C# and any other .NET language would support optional parms then I would use them as they will result in lesser and cleaner code.

Thursday, February 16, 2006 11:30 PM by budoi

# re: Default Values

i use c# and i never wanted this feature. overloading ako since OOP. have you looked at EII? that's one of the c# features that i liked...

Friday, February 17, 2006 12:12 AM by jokiz

# re: Default Values

ngaun ko lang narining yang EII ah.. ano po yan? Electronic Information Interchange? :P

Friday, February 17, 2006 9:21 PM by bonskijr

# re: Default Values

EII = Enterprise Information Integration :)

Saturday, February 18, 2006 8:43 PM by keithrull

# re: Default Values

explicit interface implementation

Sunday, February 19, 2006 8:21 PM by jokiz

# re: Default Values

jokiz:now it's clear, however I think it complicates it more than overloading since you have to explicitly call/typecast (to)the correct interface.. but I see were you're getting at, I just don't know if I'll ever touch EII though :)

Sunday, February 19, 2006 9:25 PM by bonskijr

# re: Default Values

programming through an interface is one of the powerful features in OOP. i'm not sure you'll touch it since it is only a feature in c#, :p

Monday, February 20, 2006 4:59 PM by jokiz

# re: Default Values

you can do interfaces in vb.net too!

Saturday, February 25, 2006 4:34 PM by keithrull