我想将一些值(最多十个)与一个将返回它们的最小值的函数进行比较。
我的同事写了这样的函数:
set @smallest = null
if @smallest is null or @date0 < @smallest
begin
set @smallest = @date0
end
if @smallest is null or @date1 < @smallest
begin
set @smallest = @date1
end
... (repeating 10 times)
除此之外,if语句可以更聪明地编写(在第一次比较后,null检查可能会消失)我想知道是否创建一个内存索引表并让函数返回第一个值会更有效吗? 有没有我可以阅读的文件?
答案 0 :(得分:3)
创建内存索引表
10条记录上没有索引。创建一个派生表(将在内存中),如下所示,然后在表中运行MIN:
select @smallest = MIN(Adate)
from (
select @date0 Adate union all
select @date1 union all
select @date2 union all
-- ....
select @date9) X