sql server实现oracle递归树函数例子(start with.....connect by prior.....)

sqlserver实现oracle递归函数的例子(start with.....connect by prior.....)
通过将查询遍历到的数据插入临时表方式实现
此函数应该在考虑下这个临时表的删除文件 以及创建时再判断表结构是否已经存在,相信这个应该很容易........
使用方法:
select * from dbo.getSubCompany(24)
返回结果集结构:

taxi_company_id int,taxi_company_name varchar(200),super_company_id int,level int
目前不支持动态表名和列名。函数定义:
create function getSubCompany(@idPart int)
returns   @tabinfo table(taxi_company_id int,taxi_company_name varchar(200),super_company_id int,level int) 
as 
begin 
 
declare @cid int 
set @cid = 1 
 
    insert @tabinfo 
    select taxi_company_id,taxi_company_name,super_company_id ,@cid from taxi_company where taxi_company_id = @idPart 

   while @@rowcount>0 
    BEGIN 
      set @cid = @cid + 1
      insert @tabinfo 
      select distinct x.taxi_company_id,x.taxi_company_name,x.super_company_id ,@cid
      from taxi_company as x join  @tabinfo as y 
      on x.super_company_id=y.taxi_company_id 
      where y.level = @cid-1
    end
   return 
end

本文转自:http://blog.csdn.net/guoquan2003/article/details/5147985



你可能感兴趣的:(sql server实现oracle递归树函数例子(start with.....connect by prior.....))