Optional parameter in SQL server

前端 未结 2 1451
我在风中等你
我在风中等你 2021-01-11 18:50

I have a user defined function which is used in many stored procedures which will return me some value. If it possible for me to add a new optional parameter to the same.

2条回答
  •  臣服心动
    2021-01-11 19:02

    If you don't want to go adjusting all of your existing stored procedures that reference the function then I think you would need to create a new function with the code from your existing one

    CREATE FUNCTION CalculateAverageForUser2
    (
        @userid int,
        @param2 nvarchar(10) = NULL
    )
    RETURNS float
    AS
    /*Code from existing function goes here*/
    

    Then just change the existing function to the following

    ALTER FUNCTION CalculateAverageForUser 
    (
     @userid int
    )
    RETURNS float
    AS
    BEGIN
    RETURN dbo.CalculateAverageForUser2(@userid, DEFAULT)
    END
    

提交回复
热议问题