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

  • 相关阅读:
    typescript中的类型兼容性
    typescript中使用泛型
    分数的乘法逆元和负数的取模运算
    pip2 install protobuf==2.6.1
    git使用代理
    Mount error(5):Input/output error on mount
    cmake 学习笔记(一)
    cmake --help
    ImportError: dynamic module does not define init function (initcaffe)
    docker
  • 原文地址:https://www.cnblogs.com/cyz1980/p/553183.html
Copyright © 2011-2022 走看看