Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « SQL Advent 2011 Day 3: PartitioningSQL Advent 2011 Day 2: system tables and catalog views »
    comments

    Someone had some trouble earlier today with calling a user defined function that has a default value for a parameter

    When you have a stored procedure with default values for parameters, you can omit those when calling the proc. With user defined functions, it works a little different, let's take a look.

    First create this simple function

    1. CREATE FUNCTION dbo.fnTest(@param1 INT, @param2 int = 1 )
    2. RETURNS int
    3. AS
    4. BEGIN
    5.    
    6.         RETURN @param2
    7.    
    8. END
    9. GO

    As you can see @param2 has a default of 1.

    Calling the function by supplying both parameters works as expected

    1. SELECT dbo.fnTest(  23,3 )

    Output
    -------------
    3

    Now try to do this

    1. SELECT dbo.fnTest(  23 )

    Here is the error message that we get back

    Msg 313, Level 16, State 2, Line 1
    An insufficient number of arguments were supplied for the procedure or function dbo.fnTest.

    If you look in books on line: http://msdn.microsoft.com/en-us/library/ms186755.aspx
    You will see the following text

    If a default value is defined, the function can be executed without specifying a value for that parameter.

    When a parameter of the function has a default value, the keyword DEFAULT must be specified when the function is called in order to retrieve the default value. This behavior is different from using parameters with default values in stored procedures in which omitting the parameter also implies the default value. An exception to this behavior is when invoking a scalar function by using the EXECUTE statement. When using EXECUTE, the DEFAULT keyword is not required.

    So, let's try that

    1. SELECT dbo.fnTest(  23, default )

    Output
    -------------
    1

    There you have it, works like a charm

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    336 views
    Instapaper

    No feedback yet

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)