Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alter a SQL server function to accept new optional parameter

I already have a function in SQL Server 2005 as:

ALTER function [dbo].[fCalculateEstimateDate] (@vWorkOrderID numeric) Returns varchar(100)  AS Begin   <Function Body> End 

I want to modify this function to accept addition optional parameter @ToDate. I am going to add logic in function if @Todate Provided then do something else continue with existing code.

I modified the function as:

ALTER function [dbo].[fCalculateEstimateDate] (@vWorkOrderID numeric,@ToDate DateTime=null) Returns varchar(100)  AS Begin   <Function Body> End 

Now I can call function as:

SELECT dbo.fCalculateEstimateDate(647,GETDATE()) 

But it gives error on following call:

SELECT dbo.fCalculateEstimateDate(647) 

as

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

which as per my understanding should not happen.

Am I missing anything? Thanks in advance.

like image 912
MaxRecursion Avatar asked Oct 04 '12 06:10

MaxRecursion


People also ask

How do you pass an optional parameter in SQL?

We can create function with default parameters. To call that function without that parameter, you need to pass NULL to that parameter. Let's say I have function with Two Parameters, in which second parameter can be optional. Let me know if it helps you in any way.

Can SQL functions have optional parameters?

Fortunately, it's pretty easy to make some parameters required and others optional. You simply give them a default value. The first three parameters are required and the next three aren't.

How do you modify a function in SQL?

Use SQL Server Management Studio Select on the plus sign next to the database that contains the function you wish to modify. Select on the plus sign next to the Programmability folder. Select the plus sign next to the folder that contains the function you wish to modify: Table-valued Function.

How do you use alter function?

You must own the function to use ALTER FUNCTION . To change a function's schema, you must also have CREATE privilege on the new schema. To alter the owner, you must also be a direct or indirect member of the new owning role, and that role must have CREATE privilege on the function's schema.


2 Answers

From CREATE FUNCTION:

When a parameter of the function has a default value, the keyword DEFAULT must be specified when the function is called 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.

So you need to do:

SELECT dbo.fCalculateEstimateDate(647,DEFAULT) 
like image 186
Damien_The_Unbeliever Avatar answered Oct 30 '22 13:10

Damien_The_Unbeliever


The way to keep SELECT dbo.fCalculateEstimateDate(647) call working is:

ALTER function [dbo].[fCalculateEstimateDate] (@vWorkOrderID numeric) Returns varchar(100)  AS    Declare @Result varchar(100)    SELECT @Result = [dbo].[fCalculateEstimateDate_v2] (@vWorkOrderID,DEFAULT)    Return @Result Begin End  CREATE function [dbo].[fCalculateEstimateDate_v2] (@vWorkOrderID numeric,@ToDate DateTime=null) Returns varchar(100)  AS Begin   <Function Body> End 
like image 20
Gus Avatar answered Oct 30 '22 13:10

Gus