I wanted to share with you the following idea.
I very love the "SQL Functions". But the big deficiency of them, That you need to explain all the parameters.
So if I have a function. and I need to add more parameter. I need to find all his callings, and to add the new parameter.
I thought to decide that any function, be contain, Let's say, 10 parameters with defaults to all unused parameter.
For that matterת I have the function "MyFN". She will be shown like that:
create function MyFN(@I int, @S nvarchar(50), @D datetime, @P4 sql_variant = null, @P5 sql_variant = null, @P6 sql_variant = null, @P7 sql_variant = null, @P8 sql_variant = null, @P9 sql_variant = null, @P10 sql_variant = null)
returns int
as
begin
return 1
end
We will call to her like that:
select dbo.MyFN(1,'xxx',getdate(),default,default,default,default,default,default,default) -- => 1
Now we need to add a new parameter - real.
Now the function will be like that:
alter function MyFN(@I int, @S nvarchar(50), @D datetime, @R real = 0, @P5 sql_variant = null, @P6 sql_variant = null, @P7 sql_variant = null, @P8 sql_variant = null, @P9 sql_variant = null, @P10 sql_variant = null)
returns int
as
begin
if @R > 0
return 2
return 1
end
And we will call her:
select dbo.MyFN(1,'xxx',getdate(),0.5,default,default,default,default,default,default) -- => 2
And we can still call her, like the previous method, and to get the same answer.
I would love to hear your thoughts on the matter!
And how much parameters you thing that need to set.
Thank you very much!
defaultdoesn't work that way. Just leave the arguments out of your call. If you add a new argument to a function the old calls will still work. Many programmers will argue that defaults are a bad idea in general.sql_variant. @inon, issues with variant types and scalar functions aside (and whether this would work as you expected) this is really not a good idea. Is there a problem you were trying to solve with this?