T-Sql ,自定义函数,返回递归结果集

写程序是总是用到父子关系的数据,通过给定节点得到其子节点的记录,写视图但是不支持传入参数。

那就用 自定义函数来完成这个需求吧!

1.创建视图

create Function myFunc(@id Int)
Returns @tab table (id int,ParentId int,[Level] int,TName nvarchar(50))
As
 begin
 
   --DECLARE @typeId int;
   --set @typeId =6;
 with cte as
 (
  select * from  tab where id= @id
  union all
  select a.* from tab a, cte b
  where a.parentid = b.id
 )
 insert @tab select id ,ParentId,[Level],TName from cte;
 return
End

 

2.表值函数调用

select * from  dbo.myFunc(6)

 

转载保留:http://write.blog.csdn.net/postedit/7661094

 

posted @ 2012-06-13 22:01  草青工作室  阅读(168)  评论(0编辑  收藏  举报