Possible Duplicate:
Is there a Max function in SQL Server that takes two values like Math.Max in .NET?可能重复:SQL Server中是否有Max函数接受两个值,比如Math。马克斯在。net ?
In Excel, there's a function called "MAX" that accepts numbers and returns the largest one in the set. Is there a function in T-SQL that duplicates this functionality? I haven't been able to find one, and I've written a UDF that does it for me, but I thought it was worth asking.
在Excel中,有一个名为“MAX”的函数,它接受数字并返回集合中最大的数字。我还没能找到,我已经写了一个UDF来帮助我,但我觉得值得问一下。
Here is the function I've been using:
这是我一直在使用的函数:
CREATE FUNCTION dbo.LargerOf
(
-- Add the parameters for the function here
@First FLOAT,
@Second FLOAT
)
RETURNS FLOAT
AS
BEGIN
DECLARE @Result FLOAT
IF @First > @Second
SET @result = @First
ELSE
SET @Result = @Second
RETURN @Result
END
GO
I don't expect any luck, but instead of moving my function to a whole bunch of new servers, I thought I'd at least ask. Thanks!
我不指望会有什么好运气,但与其把我的函数移动到一大堆新服务器上,我想我至少应该问问。谢谢!
4 个解决方案
#1
10
I don't know if the function you need exists, but for a workaround, I like this one better
我不知道你需要的功能是否存在,但对于一个变通方法,我更喜欢这个。
set @max = case when @first > @second then @first else @second end
#2
4
You could use:
您可以使用:
CASE
WHEN @First >= @Second THEN @FIRST
ELSE @Second
END
#3
4
declare @first int, @second int
声明@first int, @second int
select @first=45, @second=123
选择@first = 45岁@second = 123
select max(a) from (select @first a UNION ALL select @second) x
从(select @first a UNION ALL select @second) x中选择max(a)
--OR
——或者
select max(a) from (values (@first),(@second)) x(a)
选择max(a)(值(@first),(@second))) x(a)
#4
1
Unfortunately not.
不幸的是没有。
A word of warning, for extremely intensive usage, I've found that scalar functions (even those which could be easily inlined with a CASE
, like yours) really do not perform well on SQL Server 2005, so if you are dealing with millions of calls, put it inline (sometimes you can fake an inline TVF).
一句警告,非常密集的使用,我发现标量函数(即使是那些很容易内联的情况下,像你这样的)真正表现不佳的SQL Server 2005,所以如果你正在处理数以百万计的电话,把它内联(有时可以伪造一个内联TVF)。
Hopefully, SQL Server will eventually have an inline SVF or have a function equivalent to GREATEST
!
希望SQL Server最终会有一个内联的SVF或一个与之等价的函数!
#1
10
I don't know if the function you need exists, but for a workaround, I like this one better
我不知道你需要的功能是否存在,但对于一个变通方法,我更喜欢这个。
set @max = case when @first > @second then @first else @second end
#2
4
You could use:
您可以使用:
CASE
WHEN @First >= @Second THEN @FIRST
ELSE @Second
END
#3
4
declare @first int, @second int
声明@first int, @second int
select @first=45, @second=123
选择@first = 45岁@second = 123
select max(a) from (select @first a UNION ALL select @second) x
从(select @first a UNION ALL select @second) x中选择max(a)
--OR
——或者
select max(a) from (values (@first),(@second)) x(a)
选择max(a)(值(@first),(@second))) x(a)
#4
1
Unfortunately not.
不幸的是没有。
A word of warning, for extremely intensive usage, I've found that scalar functions (even those which could be easily inlined with a CASE
, like yours) really do not perform well on SQL Server 2005, so if you are dealing with millions of calls, put it inline (sometimes you can fake an inline TVF).
一句警告,非常密集的使用,我发现标量函数(即使是那些很容易内联的情况下,像你这样的)真正表现不佳的SQL Server 2005,所以如果你正在处理数以百万计的电话,把它内联(有时可以伪造一个内联TVF)。
Hopefully, SQL Server will eventually have an inline SVF or have a function equivalent to GREATEST
!
希望SQL Server最终会有一个内联的SVF或一个与之等价的函数!