---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

SQL SERVER 2000 遍历父子关系数据的表(二叉树)获得所有子节点 所有父节点及节点层数函数的更多相关文章

  1. SQL SERVER 2000/2005/2008数据库数据迁移到Oracle 10G细述

    最近参与的一个系统涉及到把SQL Server 2k的数据迁移到Oracle 10G这一非功能需求.特将涉及到相关步骤列举如下供大家参考: 环境及现有资源: 1.OS: Windows 7 Enter ...

  2. SQL SERVER 2000 如何提高大数据筛选GROUP BY 的效率

    数据库有83W条记录,本想计算20180101之后的每天赔付情况,故写了以下SQL语句: SELECT 起保时间,sum(赔付金额) as 日赔付 FROM maindata WHERE 起保时间&g ...

  3. Sql Server合并多行询数据到一行:使用自连接、FOR XML PATH('')、STUFF或REPLACE函数

    示例表 tb 数据如下 id value ----- 1 aa 1 bb 2 aaa 2 bbb 2 ccc SELECT id, [val] = ( SELECT [value] + ',' FRO ...

  4. Windows 7 X64 SQL Server 2000 企业管理器无法建立新表

    问题现象: 当建立新表时会出现一个空白的窗口,敲击键盘输入后就会出现应用出错,然后退出. 解决方案: 选中某个已经存在的表,再点右键,新建表就可以正常操作了.

  5. SQL Server 2000向SQL Server 2008 R2推送数据

    [文章摘要]最近做的一个项目要获取存在于其他服务器的一些数据,为了安全起见,采用由其他“服务器”向我们服务器推送的方式实现.我们服务器使用的是SQL Server 2008 R2,其他“服务器”使用的 ...

  6. Sql Server 2000/2008 用Sql导出数据表结构(包括注释)到Excel

    --Sql Server 2000 declare @id int, ), ) declare cur_1 cursor for select id,name from sysobjects wher ...

  7. SQL Server 2000服务器安装剖析

    一.情况说明 sql server 2000以前的版本,例如7.0一般不存在多个版本,只有标准版跟桌面版,用户如果不清楚该装什么版本的话,可按安装上的安装先决条件指示安装,一般在WIN2000 服务器 ...

  8. SQL SERVER 2000安装遇到的问题小汇总(转载)

    [1]安装程序配置服务器失败需要修改下注册表1 打开注册表 在"开始"--"运行"键入 "regedit"  2 删除注册表如下键值: HK ...

  9. sql server 2000,Log.LDF文件丢失,附加数据库失败的解决办法[转]

    SQL Server数据库备份有两种方式,一种是使用BACKUP DATABASE将数据库文件备份出去,另外一种就是直接拷贝数据库文件mdf和日志文件ldf的方式.下面将主要讨论一下后者的备份与恢复. ...

随机推荐

  1. Revenge of GCD HDU5019

    Description In mathematics, the greatest common divisor (gcd), also known as the greatest common fac ...

  2. XPath相关笔记

    <?xml version="1.0" encoding="utf-8" ?> <employees>   <employee o ...

  3. Windows中 RabbitMQ安装与环境变量配置

    RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议.1:安装RabbitMQ需要先安装Erlang语言开发包.下载地址 ht ...

  4. 扩展KMP——算法总结,来自于 迷路的鸽子

    扩展kmp                 LRH 所谓扩展kmp指的是与kmp相似的求辅助数组的原理,但是本身与kmp关系不大. 1.exkmp的用途:给定一个主串s和一个子串t,求出s中每一个后缀 ...

  5. Generating Faces with Deconvolution Networks

    用深度学习做人脸合成,website:https://zo7.github.io/blog/2016/09/25/generating-faces.html 受启发于 Learning to Gene ...

  6. Android应用程序组件介绍

    应用程序组件是Android应用程序的基本构建单元.每个组件是系统进入你的应用程序的不同入口点.不是所有的组件对于用户都是实际上的入口点,有些是互相依赖的,但是每个组件都有特定的作用——每个都是唯一的 ...

  7. sublime text3编译运行C,Java程序的一些配置

    环境:linux 64位 桌面环境: gnome Java编译运行 (1)Preferences --> Browse Packages --> 在该文件夹下新建build文件如: Myj ...

  8. PAT 团体程序设计天梯赛-练习集 L1-023. 输出GPLT

    给定一个长度不超过10000的.仅由英文字母构成的字符串.请将字符重新调整顺序,按“GPLTGPLT....”这样的顺序输出,并忽略其它字符.当然,四种字符(不区分大小写)的个数不一定是一样多的,若某 ...

  9. Token验证失败的解决方法

    Token验证失败 微信 微信公众平台开发 Token校验失败 URL Token原文 http://www.cnblogs.com/txw1958/p/token-verify.html Token ...

  10. javascript的insertBefore、insertAfter和appendChild简单介绍

      target.insertBefore(newChild,existingChild)参数说明:1.target:被添加节点和现有节点的父节点.2.newChild:将要被插入的节点.3.exis ...