• (原创)基于关系数据库系统链式存储的树型结构数据,求某结点下的子树所有结点算法(tsql语言实现)


    (原创)基于关系数据库系统链式存储的树型结构数据,求某结点下的子树所有结点算法(t-sql语言实现) 

    CREATE FUNCTION  f_subtree(@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 *  from dpet    
      insert into @t  select * from @temp where dpcode_p=@dpcode  
      insert into @new  select * from @t  --结果临时表
      while (exists(select * from @t where dpcode in (select dpcode_p from @temp)))
        --当某层的结点全为叶子时,才停止循环
        begin
          insert into @tt select * from @t --中间交换临时表
          delete @t
          insert into @t select * from @temp where dpcode_p in
            (select dpcode from @tt)
          delete @tt
          insert into @new select * from @t
        end
      insert into @new select * from @temp where dpcode=@dpcode 

    return
    END

    ---------------------------------------------------(以下为存储过程实现) 

    CREATE TABLE dpet (   --树型结构数据的数据存放表结构
     [dpcode] [varchar] (9)  ,     --结点编码
     [dpname] [varchar] (20) ,  --结点名称
     [dpcode_p] [varchar] (9)   --此结点的父结点编码
    ) ON [PRIMARY]

    /*ms sql 2000调试通过,表temp,t,new,tt 结构与表dpet一致*/

    CREATE PROCEDURE  desc_dept(@dpcode varchar(9))   AS
    --树型结构数据,求某结点下的的子树所有结点
      truncate table temp
      truncate table t
      truncate table tt
      truncate table new
      insert into temp select *  from dpet    --数据临时存放表
      insert into t  select * from temp where dpcode_p=@dpcode  --中间临时表
      insert into new  select * from t  --结果临时表
      while (exists(select * from t where dpcode in (select dpcode_p from temp)))
        --当某层的结点全为叶子时,才停止循环
        begin
          insert into tt select * from t --中间交换临时表
          truncate table t
          insert into t select * from temp where dpcode_p in
            (select dpcode from tt)
          truncate table tt
          insert into new select * from t
        end
      insert into new select * from temp where dpcode=@dpcode
    GO

  • 相关阅读:
    LM算法学习笔记(一)
    USB3.0剖析(锆石科技FPGA)
    USB2.0系列(锆石科技FPGA)
    异步FIFO
    总线时钟同步
    [已解决]Mac下Anaconda-Navigator闪退问题
    [已解决]Mac下运行spyder提示"Python 意外退出"
    博客园账号被盗
    unhandled event loop exception解决方案
    初识ListView
  • 原文地址:https://www.cnblogs.com/cyz1980/p/317380.html
Copyright © 2020-2023  润新知