1

I've got a SQL Function which has 3 parameters. The 3rd parameter has a default value of NULL. However, I cannot just write:

dbo.myFunction(Param1, Param2)

I get the error:

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

Therefore I've got to write:

dbo.myFunction(Param1, Param2, NULL)
dbo.myFunction(Param1, Param2, default)

Is there a way I can just write dbo.myFunction(Param1, Param2)? I think this is a lot cleaner (and saves me having to modify an existing function which I've added a new param to!)

1 Answer 1

3

They are default parameters as opposed to optional parameters & must always be passed in the call with a value or default.

If you wanted to you could make your existing dbo.myFunction(Param1, Param2) call dbo.myFunctionEx(Param1, Param2, default)

Sign up to request clarification or add additional context in comments.

5 Comments

Is there anyway of having an optional parameter? If a new parameter is added to an existing UDF, whats the standard protocol for the existing calls to that function? Just go through them all and add 'default'?
If you call a function with too few or too many arguments you will get an error so you must either modify the existing calls to include default or create the revised proc with a different name
Thanks Alex, do you know what the reason is behind this? Seems to me like theres no reason why default cannot just be 'assumed' if its not entered.
I don't know if there is a reason other than to keep parsing rules simplified, and that's the way it's always worked. If you really, really, really want an overload to save you a few keystrokes, you could just create a second function that takes the first two parameters and calls the original function, adding the third parameter as NULL. As Alex rightly points out, these are default parameters, not optional parameters.
@Aaron It's not so much to save a few keystrokes, as this function has been called about 50 times, and I'm concerned about missing a few out, causing errors all over the application. However, this sounds like a better long term solution than creating a second function. Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.