zoukankan      html  css  js  c++  java
  • SQL SERVER 2000 遍历父子关系数据的表(二叉树)获得所有子节点 所有父节点及节点层数函数

    ---SQL SERVER 2000 遍历父子关系數據表(二叉树)获得所有子节点 所有父节点及节点层数函数
    ---Geovin Du 涂聚文
    --建立測試環境
    Create Table GeovinDu
    ([ID] Int,
     fatherID Int,
     [Name] Varchar(10)
    )
    Insert A Select 1,        0,       '中国'
    Union All Select 2,        1,          '广东'
    Union All Select 3,        1,          '北京'
    Union All Select 4,        2,          '深圳特区'
    Union All Select 5,        2,          '广州'
    Union All Select 6,        4,          '罗湖'
    Union All Select 7,        4,          '福田'
    Union All Select 8,        7,           '华强北'
    Union All Select 9,        0,  '美国'
    Union All Select 10,       9,         '华盛顿州'
    GO
    --建立函數
    
    --取字子节点
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetChildren]') and xtype in (N'FN', N'IF', N'TF'))
    drop function [dbo].[GetChildren]
    GO
    Create Function GetChildren(@ID Int)
    Returns @Tree Table (ID Int, fatherID Int, Name Varchar(10))
    As
    Begin
    Insert @Tree Select ID, fatherID, Name From GeovinDu Where fatherID = @ID
    While @@Rowcount > 0
    Insert @Tree Select A.ID, A.fatherID, A.Name From GeovinDu A Inner Join @Tree B On A.fatherID = B.ID And A.ID Not In (Select ID From @Tree)--- 
    Return
    End
    GO
    
    --取父节点
    
    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    GO
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetParent]') and xtype in (N'FN', N'IF', N'TF'))
    drop function [dbo].[GetParent]
    GO
    Create Function [dbo].[GetParent](@ID Int)
    Returns @Tree Table (ID Int, fatherID Int, Name Varchar(10))
    As
    Begin
    Insert @Tree Select ID, fatherID, Name From GeovinDu Where ID = @ID
    While @@Rowcount > 0
    Insert @Tree Select A.ID, A.fatherID, A.Name From GeovinDu A Inner Join @Tree B On A.ID = B.fatherID And A.ID Not In (Select ID From @Tree)
    Return
    End
    
    ---分为几级
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_getlevel]') and xtype in (N'FN', N'IF', N'TF'))
    drop function [dbo].[f_getlevel]
    GO
    create function f_getlevel(@id INT) returns int
    as
    begin
      declare @re_str as varchar(3)
      set @re_str = ''
      declare @level as int
      set @level = 1
      select @re_str = fatherID from GeovinDu where [ID] = @id 
      while exists (select 1 from GeovinDu where [ID] = @re_str)
        begin
          select @re_str = fatherID from GeovinDu where [ID] = @re_str
          set @level = @level + 1
        end
      return @level
    end
    go
    
    
    
    
    --測試
    SELECT * FROM GeovinDu
    GO
    
    Select * From dbo.GetChildren(4)
    
    Select * From dbo.GetParent(4)
    GO
    
    select * , dbo.f_getlevel([ID])  'level' from A
    
    drop function dbo.f_getlevel
    
    哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)成功.---Geovin Du(涂聚文)
  • 相关阅读:
    MySQL 8.0.14版本新功能详解
    Uncaught TypeError: Right-hand side of 'instanceof' is not an object
    程序员快速技术提升之道
    Windows 10 cmd命令符的使用
    数据千万条,备份第一条:VFEmail被擦除所有数据面临关停
    netty-socketio 示例代码
    idea中 在接口中如何直接跳转到该接口的是实现类中?
    perl DBD处理超时问题
    springboot 启动配置文件配置
    Office Word 发布文章到博客园
  • 原文地址:https://www.cnblogs.com/geovindu/p/2104371.html
Copyright © 2011-2022 走看看