我写了一个函数:
ALTER function [dbo].[f_get_loginName_relate_personnel] (@LoginName varchar(300))
returns varchar(300)
as begin
declare @personnel varchar(300)
if @LoginName is not null
select @personnel=name from t_bs_personnel where [email protected]
else
set @personnel=isnull(@personnel,@LoginName)
if @personnel=''
set @personnel=isnull(@personnel,@LoginName)
return @personnel
end
然后我在其他的存储过程调用这个函数:
select i.* ,
isnull(dbo.f_get_loginName_relate_personnel(user_name()), user_name()) as printer,
isnull(dbo.f_get_loginName_relate_personnel(i.checking), i.checking ) as checkings,
isnull(dbo.f_get_loginName_relate_personnel(i.lister),i.lister ) as listers,
isnull(dbo.f_get_loginName_relate_personnel(i.auditing), i.auditing) as auditings,
isnull(dbo.f_get_loginName_relate_personnel(i.cancellation), i.cancellation) as cancellations
from t_wh_billofdocument as i
现在有一个问题,我在sql语句调用这个函数时,如果数据量少倒没什么,速度是很快的,如果数据量一多起来,比如才4万行数据时,速度就会很慢了,大概需要5、6分钟,我觉得是sql语句调用了函数的问题,使得速度变慢的,有没有其它的方法,不用函数来解决我的问题?
------解决方案--------------------
------解决方案--------------------
我们来分析一下你的函数:
- SQL code
ALTER function [dbo].[f_get_loginName_relate_personnel] (@LoginName varchar(300)) returns varchar(300)as begin declare @personnel varchar(300)[email protected] 不为 NULL if @LoginName is not null[email protected] select @personnel=name from t_bs_personnel where [email protected] else--否则,[email protected]@loginname 可是,[email protected]! 把NULL替换为NULL?! set @personnel=isnull(@personnel,@LoginName)[email protected] if @personnel=''[email protected],让它等于 @Loginname 可是,既然它为空字符串,怎么还会是NULL呢?! NULL<>'' set @personnel=isnull(@personnel,@LoginName) return @personnelend