+ -
当前位置:首页 → 问答吧 → 有关sql函数的问题

有关sql函数的问题

时间:2011-12-17

来源:互联网

我写了一个函数:
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 loginame=@LoginName
  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语句调用了函数的问题,使得速度变慢的,有没有其它的方法,不用函数来解决我的问题?
   

作者: fstao   发布时间: 2011-12-17

自定义函数效率确实不佳。

补过你这个好像没必要吧

写个case when 都差不多了吧

作者: Beirut   发布时间: 2011-12-17

引用 1 楼 beirut 的回复:

自定义函数效率确实不佳。

补过你这个好像没必要吧

写个case when 都差不多了吧


用case 如何写这个语句?

作者: fstao   发布时间: 2011-12-17

引用 2 楼 fstao 的回复:

引用 1 楼 beirut 的回复:

自定义函数效率确实不佳。

补过你这个好像没必要吧

写个case when 都差不多了吧


用case 如何写这个语句?

洗个inner join 还有你写那么多 isnull 干什么

作者: Beirut   发布时间: 2011-12-17

引用 3 楼 beirut 的回复:

引用 2 楼 fstao 的回复:

引用 1 楼 beirut 的回复:

自定义函数效率确实不佳。

补过你这个好像没必要吧

写个case when 都差不多了吧


用case 如何写这个语句?

洗个inner join 还有你写那么多 isnull 干什么

具体如何写?

作者: fstao   发布时间: 2011-12-17