SQL自定义函数问题-特急,在线等
自定义函数如下(不完整):
CREATE function fn_GetRegDays (@RegTime smalldatetime, @GoTime smalldatetime)
returns smalldatetime
as
begin
declare @NightTimeStr varchar(5)
declare @NoonTimeStr varchar(5)
declare @AfterNoonTimeStr varchar(5)
declare @Now datetime
declare @NightTime smalldatetime
declare @CalcTime smalldatetime
SELECT @Now = now from V_NOW
select @NightTimeStr = fld_nighttime, @NoonTimeStr=fld_noontime, @AfterNoonTimeStr=fld_afternoontime from tbl_company
SET @NightTime = cast(cast(datepart(yyyy, @RegTime) as varchar) + '- ' + cast(datepart(mm, @RegTime) as varchar) + '- ' + cast(datepart(dd,@RegTime) as varchar) + ' ' + @NightTimeStr as smalldatetime)
SET @CalcTime = cast(cast(datepart(yyyy, @RegTime) as varchar) + '- ' + cast(datepart(mm, @RegTime) as varchar) + '- ' + cast(datepart(dd,@RegTime) as varchar) + ' ' + @NoonTimeStr as smalldatetime)
if (datediff(n, @RegTime, @NightTime) <0)
begin
SET @CalcTime = dateadd(dd, 1, @CalcTime)
end
return @CalcTime
end
然后我的查询如下:
select *, dbo.fn_GetRegDays(fld_regdt, fld_godt) From tbl_reginfo
假若这个SQL查询有1000条记录,那那个fn_GetRegDays 函数是执行一次还是1000次,
如果是执行1000次,如果执行1000次那不是效率很低,因为我自定义函数里面有好几个Select语句啊...如何改进?
------解决方案--------------------关联获取出函数中的值,在计算。。。~~
个人看法。。