zoukankan      html  css  js  c++  java
  • 树型结构数据,求某结点的所有父结点的自定义函数

               /*树型结构数据,求某结点的所有父结点的自定义函数*/
    CREATE FUNCTION  f_father(@dpcode varchar(9)) 
    RETURNS  @new table([dpcode] [varchar] (9)  ,     --结点编码
     [dpname] [varchar] (20) ,  --结点名称
     [dpcode_p] [varchar] (9) --此结点的父结点编码
    )  AS 
    BEGIN
    declare @temp table([dpcode] [varchar] (9)  ,     --结点编码
     [dpname] [varchar] (20) ,  --结点名称
     [dpcode_p] [varchar] (9)   --此结点的父结点编码
    )  --数据临时存放表
    declare @t table([dpcode] [varchar] (9)  ,     --结点编码
     [dpname] [varchar] (20) ,  --结点名称
     [dpcode_p] [varchar] (9)   --此结点的父结点编码
    )   --中间临时表
    declare @tt table([dpcode] [varchar] (9)  ,     --结点编码
     [dpname] [varchar] (20) ,  --结点名称
     [dpcode_p] [varchar] (9)   --此结点的父结点编码
    )  --中间交换临时表


    delete   @temp
    delete  @t
    delete @tt
    delete  @new


    insert into @temp select DPTCODE, DPTNAME, DPTCODE_P  from base_dept   
      insert into @t  select * from @temp where dpcode=@dpcode 
      insert into @new  select * from @t  --结果临时表
      while ( 1=1)
      
        begin
        if (exists(select * from @t where dpcode ='001'))  --当为根时,停止循环
         begin
           break
         end   
          insert into @tt select * from @t --中间交换临时表
          delete @t
          insert into @t select * from @temp where dpcode in
            (select dpcode_p from @tt)
          delete @tt
          insert into @new select * from @t  
       end
      --insert into @new select * from @temp where dpcode=@dpcode 

    return
    END

  • 相关阅读:
    iscsi: 多路径
    Paxos算法分析
    ceph实践: 搭建环境
    ocfs2: 搭建环境
    设计模式:Context模式
    Ceph剖析:Leader选举
    Ceph剖析:定时器safetimer的实现
    nfs:环境搭建
    Ceph剖析:数据分布之CRUSH算法与一致性Hash
    Linux命令小结:crontab/netstat/iostat/sar
  • 原文地址:https://www.cnblogs.com/cyz1980/p/553183.html
Copyright © 2011-2022 走看看