我有一个用户定义的函数,在许多存储过程中使用它将返回一些值.如果我可以添加一个新的可选参数.
如果我没有传递任何值,它应该为null,如果我传递一些值,它应该采取它.我不想去更改所有存储过程来执行此操作.
示例代码
dbo.CalculateAverageForUser(userid int)
Run Code Online (Sandbox Code Playgroud)
我可以用吗 dbo.CalculateAverageForUser(userid int, type NVARCHAR(10) = NULL)
Mar*_*ith 12
如果您不想调整引用该函数的所有现有存储过程,那么我认为您需要使用现有存储过程中的代码创建一个新函数
CREATE FUNCTION CalculateAverageForUser2
(
@userid int,
@param2 nvarchar(10) = NULL
)
RETURNS float
AS
/*Code from existing function goes here*/
Run Code Online (Sandbox Code Playgroud)
然后只需将现有功能更改为以下内容即可
ALTER FUNCTION CalculateAverageForUser
(
@userid int
)
RETURNS float
AS
BEGIN
RETURN dbo.CalculateAverageForUser2(@userid, DEFAULT)
END
Run Code Online (Sandbox Code Playgroud)