sql自定義函數 包含遊標

 1 --传入字符串参数,返回字符串
 2 CREATE FUNCTION PMselect(@pProID nvarchar(40))
 3 RETURNS nvarchar(50AS  
 4 BEGIN 
 5 declare @pList nvarchar(50)
 6 declare @pm nvarchar(10)
 7 --定义游标
 8 declare pm_cursor  cursor  for 
 9  select PersonName from TblPerson where OID in (select User_ID from TblProManager where Pro_ID=@pProID
10 
11 --打开游标,获取内容存入@pm
12 open pm_cursor 
13 fetch next from pm_cursor into @pm
14 set @pList=''
15 
16 --如果有数据就循环
17 while @@FETCH_STATUS =0
18 begin
19 if(@pList = '')
20 set @pList= @pm
21 else
22 set @pList=@pList+',' + @pm
23 fetch next  from pm_cursor  into @pm
24 
25 end
26 
27 --关闭游标
28 close pm_cursor
29 deallocate pm_cursor
30 
31 --返回结果
32 return @pList
33 
34 
35 END
36 
37 
原文地址:https://www.cnblogs.com/jxcia_Lai/p/1832734.html