SQL自定义函数调用实例

SQL自定义函数是SQL函数中用途最广的函数之一,下面就将为您介绍如何调用SQL自定义函数,供您参考,希望对您有所启迪。

 
 
 
  1. --根据组织,岗位和档案ID返回该用户可见的日报列表  
  2.  
  3. ALTER proc dbo.GetDailyList @orgid int,@postid int,@ArchivesID int  
  4. as  
  5.     declare @a table(orgid int)  
  6.     insert @a select orgid from organize where orgid = @orgid  
  7.     while @@rowcount > 0   --中间有一递归,有点难懂啊  
  8.         insert @a select a.orgid from organize as a inner join @a as b  
  9.         on a.fatherorgid = b.orgid and a.orgid not in(select orgid from @a)  
  10.  
  11.     declare @b table(postid int)  
  12.     insert @b select postid from post where postid = @postid  
  13.     while @@rowcount > 0  
  14.         insert @b select a.postid from post as a inner join @b as b  
  15.         on a.fatherpostid = b.postid and a.postid not in(select postid from @b)  
  16.  
  17. --declare @ArchivesID nvarchar(20)  
  18. --select @ArchivesIDArchivesID=ArchivesID from Users where UserID=@userid  
  19. --print @ArchivesID  
  20.  
  21.  
  22. SELECT a.DailyID, a.TaskTitle,a.AuthorID,b.Name as AuthorName,a.DispathchManID,  
  23.         dbo.GetArchivesNameStr(a.DispathchManID,',') AS DispatchManName,   
  24.         a.AddDate, a.hit,dbo.GetCommentCount(a.DailyID) AS commentcount,  
  25.         'StateStr'=case   
  26.                     when a.StateID=1 then '未执行'  
  27.                     when a.StateID=2 then '执行中'  
  28.                     when a.StateID=3 then '执行完成'  
  29.                 end,  
  30.         'ImportTypeName'=case   
  31.                     when a.ImportTypeID=1 then '普通'  
  32.                     when a.ImportTypeID=2 then '紧急'  
  33.                     when a.ImportTypeID=3 then '非常紧急'  
  34.                 end, dbo.GetArchivesNameStr(FinishManID,',') as FinishManName,  
  35.                 dbo.GetArchivesNameStr(FactDispatchManID,',') as FactDispatchManName  
  36. FROM DailyList a LEFT  JOIN  Archives b on a.AuthorID=b.ArchivesID   
  37. WHERE (a.DailyType = 1) AND   
  38.     b.orgid IN (select orgid from @a) and   
  39.     ','+a.DispathchManID like '%,'+convert(nvarchar,@ArchivesID)+',%' 

 

 

 

【编辑推荐】

SQL自定义函数实例

初级sql select语法

sql server死锁的检测方法

SQL SERVER字段类型的说明

SQL Server安全解析

THE END