Development Articles
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsOtherDevelopment Articles

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old November 9th, 2004, 05:12 AM
sivakumar.k sivakumar.k is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 4 sivakumar.k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up Using Default parameter values for T-SQL stored procedures

Setting default values to stored procedure parameters is quite a useful feature, especially when the same stored procedure is called in different contexts. This saves the programmers from sending some dummy values to the procedure when the parameter is irrelevant in the present context. Minimizing the programmer’s effort by sending only the required parameters to the procedure is not the sole purpose of using default parameters.

When a stored procedure is already in production and some part of your application needs to send additional parameters to enhance the result from the procedure, it is always advisable to use default parameters, so that other part of the application that is happily using this procedure with the existing parameters will not be affected by this change. You are saved from the headache of finding out all the code that uses this procedure and sending dummy values when it is not necessary.
Syntax:

Create Procedure DefaultParamProcedure ( @Param1 int,
@Param2 varchar(20),
@Param3 int = NULL)
As
Begin

-- Some queries

End

Calling the above procedure from another procedure

ExeDefaultParamProcedure 1,’xyz’c

It is always advisable to use the default parameters at tail of the parameter list so that the actual parameters supplied are passed to the relevant formal parameters. In case if you want to send values to the trailing default parameters and let the leading parameters to use the default values use formal parameter names to assign values to them.

Syntax:

Create Procedure DefaultParamProcedure ( @Param1 int,
@Param2 varchar(20) = ‘xyz’,
@Param3 int = NULL)
As
Begin
-- some queries

End

Calling the above procedure from another procedure

Exec DefaultParamProcedure 1, @Param3 = 5

This will assign ‘1’ to @Param1 and ‘5’ to @Param3 and @Param2 will use the default value ‘xyz’ assigned to it.

An interesting point is, even the “Output” parameters can have default values. In some cases you may use output parameters to get some values from the stored procedure apart from the result set. For example, the number of records returned by the procedure. Not all the scenarios will need that data. In that case you can simply omit the use of that output parameter by using default values.

Syntax:

Create Procedure DefaultParamProcedure ( @Param1 int,
@Param2 varchar(20) = ‘xyz’,
@Param3 int = NULL Output)
As
Begin

-- some queries

End

Calling the above procedure from another procedure

Exec DefaultParamProcedure 1

The same is the case when you are calling the procedure from the front-end code that you use. All you have to do is supply only the required parameters for the procedure. Rest you can simply omit if they are irrelevant in the present call.

Reply With Quote
Reply

Viewing: ASP Free ForumsOtherDevelopment Articles > Using Default parameter values for T-SQL stored procedures


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT