Nic*_*ick 25 sql t-sql sql-server sql-server-2005
我知道T-SQL不是面向对象的.我需要编写一组模拟C#中方法重载的函数.
是否在T-SQL中以任何方式支持函数重载?如果有黑客这样做,建议吗?
Joh*_*ers 11
不,没有办法做到这一点.
我建议你重新审视这个要求,因为"让苹果看起来像橙子"通常很难做到,而且值得怀疑.
我成功完成的一件事是以允许它处理空值的方式编写函数,然后使用null来调用它来代替您想要省略的参数.
例:
create function ActiveUsers
(
@departmentId int,
@programId int
)
returns int
as
begin
declare @count int
select @count = count(*)
from users
where
departmentId = isnull(@departmentId, departmentId)
and programId = isnull(@programId, programId)
return @count
end
go
Run Code Online (Sandbox Code Playgroud)
用途:
select ActiveUsers(1,3) -- users in department 1 and program 3
select ActiveUsers(null,3) -- all users in program 3, regardless of department
select ActiveUsers(null,null) -- all users
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16245 次 |
| 最近记录: |