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

it2022-05-05  102

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

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

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

 

转载于:https://www.cnblogs.com/xxj-jing/archive/2012/06/13/2890061.html

相关资源:Microsoft SQL SERVER 2008技术内幕 T-SQL查询

最新回复(0)