我知道T-SQL不是面向对象的。我需要编写一组函数来模拟C#中的方法重载。
T-SQL是否以任何方式支持函数重载?如果有黑客可以做到这一点,建议这样做吗?
发布于 2009-06-27 15:17:06
不,没有办法做到这一点。
我建议你重新考虑这个要求,因为“让苹果看起来像橙子”通常很难做到,而且价值值得怀疑。
发布于 2011-01-07 07:00:53
我已经成功地完成了一件事,就是以允许它处理空值的方式编写函数,然后用空值代替您想要省略的参数来调用它。
示例:
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用途:
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发布于 2009-06-26 19:14:22
你可以传入一个sql_variant,但是它周围有各种各样的危险;你不能像在OO语言和重载中那样使用强类型。
如果需要在函数中查找基类型,可以使用SQL_VARIANT_PROPERTY函数。
https://stackoverflow.com/questions/1050700
复制相似问题