运用 fetch

ALTER function [dbo].[GetOwnerString](
@Id int)
returns nvarchar(200)
as
begin
 declare @result nvarchar(200),@owner nvarchar(20);
 
 set @result='';
 declare owner_cursor cursor for select Owner from InProcess where ProjectId=@Id;
 
 open owner_cursor;
 
 fetch next from owner_cursor into @owner;
 
 while(@@FETCH_STATUS=0)
  begin
   if(@result='')
    set @result=@owner;
   else
    set @result=@result+','+@owner;
   
   fetch next from owner_cursor into @owner;
  end
 
 close owner_cursor;

 deallocate owner_cursor;
 
 return @result;
 
end

 

对于函数, 做一些简单逻辑操作还是可以的,对性能的影响不大. 在涉及多表操作时,就应该注意表间查询带来的执行时间的消耗.

posted @ 2011-11-08 11:07  Yu  阅读(164)  评论(0编辑  收藏  举报