Alter a SQL server function to accept new optional parameter

后端 未结 3 1658
Happy的楠姐
Happy的楠姐 2020-12-05 01:14

I already have a function in SQL Server 2005 as:

ALTER function [dbo].[fCalculateEstimateDate] (@vWorkOrderID numeric)
Returns varchar(100)  AS
Begin
  

        
相关标签:
3条回答
  • 2020-12-05 01:50

    I have found the EXECUTE command as suggested here T-SQL - function with default parameters to work well. With this approach there is no 'DEFAULT' needed when calling the function, you just omit the parameter as you would with a stored procedure.

    0 讨论(0)
  • 2020-12-05 02:11

    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)
    
    0 讨论(0)
  • 2020-12-05 02:17

    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
    
    0 讨论(0)
提交回复
热议问题