---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. BeautifulSoup抓取列表页锚文本

    素闻BeautifulSoup提取效率低,艾玛,第一印象果然是很要命的,反正比Re 和 Lxml 是要慢的,不过就无奈Re的正则折腾来折腾去,没写出来,Lxml 的 Xpath 又用得不好. 不过就这 ...

  2. USACO 2.3 Cow Pedigrees

    Cow Pedigrees Silviu Ganceanu -- 2003 Farmer John is considering purchasing a new herd of cows. In t ...

  3. python连接MongoDB

    1.安装pymongo库 windows下: pip install pymongo 或者 easy_install install pymongo 2.使用pymongo模块连接mongoDB数据库 ...

  4. git 和 svn

    GIT和SVN之间的五个基本区别 如果你在读这篇文章,说明你跟大多数开发者一样对GIT感兴趣,如果你还没有机会来试一试GIT,我想现在你就要了解它了. GIT不仅仅是个版本控制系统,它也是个内容管理系 ...

  5. xlrd doc

    The xlrd Module A Python module for extracting data from MS Excel ™ spreadsheet files. Version 0.7.3 ...

  6. ggplot2 scale相关设置-坐标转换

    ggplot2 scale相关设置-坐标转换 在R中坐标轴转换有多种形式,包括对数转换,平方根转换以及坐标刻度前后进行调换 用到的函数分别有: scale_x_log10(...) scale_y_l ...

  7. linux deepin 12.12.1和Windows 7双系统修改默认启动项

    1.打开终端,输入 sudo gedit /etc/default/grub ,然后输入密码.2.在出来的文本编辑器中找到 GRUB_DEFAULT=0 一行,把0改为2(因为我的Windows 7在 ...

  8. 更好列表页中一个航班.先unset删除数组中一个键值对,再追加,最后按键排序

    <?php $arr = array( '0' => array('item' => array( 'aa' => 'aaa', 'bb' => 'bbb' )), '1 ...

  9. Lynis 2.2.0 :面向Linux系统的安全审查和扫描工具

    Lynis是一款功能非常强大的开源审查工具,面向类似Unix/Linux的操作系统.它可以扫描系统,查找安全信息.一般的系统信息.已安装软件及可用软件信息.配置错误.安全问题.没有设密码的用户帐户.错 ...

  10. 解决XCode插件在XCode6.4上失效的办法

    Xcode 6.4  解决 插件失效的方法 查看 插件目录: ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/ 邮件打开插件 ...