树形数据查询示例
来源:互联网 发布:上海驾校知乎 编辑:程序博客网 时间:2024/05/17 12:54
--树形数据查询示例
--作者: 邹建
if exists (select * from dbo.sysobjects where id = object_id(N'[tb]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [tb]
GO
--示例数据
create table [tb]([id] int identity(1,1),[pid] int,name varchar(20))
insert [tb] select 0,'中国'
union all select 0,'美国'
union all select 0,'加拿大'
union all select 1,'北京'
union all select 1,'上海'
union all select 1,'江苏'
union all select 6,'苏州'
union all select 7,'常熟'
union all select 6,'南京'
union all select 6,'无锡'
union all select 2,'纽约'
union all select 2,'旧金山'
go
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_cid]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_cid]
GO
/*--树形数据处理
查询指定id的所有子
--邹建 2003-12(引用请保留此信息)--*/
/*--调用示例
--调用(查询所有的子)
select a.*,层次=b.[level] from [tb] a,f_cid(2)b where a.[id]=b.[id]
--*/
create function f_cid(
@id int
)returns @re table([id] int,[level] int)
as
begin
declare @l int
set @l=0
insert @re select @id,@l
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.[id],@l
from [tb] a,@re b
where a.[pid]=b.[id] and b.[level]=@l-1
end
/*--如果只显示最明细的子(下面没有子),则加上这个删除
delete a from @re a
where exists(
select 1 from [tb] where [pid]=a.[id])
--*/
return
end
go
--调用(查询所有的子)
select a.*,层次=b.[level] from [tb] a,f_cid(2)b where a.[id]=b.[id]
go
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_pid]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_pid]
GO
/*--树形数据处理
查询指定id的所有父
--邹建 2003-12(引用请保留此信息)--*/
/*--调用示例
--调用(查询所有的父)
select a.* from [tb] a,f_pid(7)b where a.[id]=b.[id]
--*/
create function f_pid(
@id int
)returns @re table([id] int,[level] int)
as
begin
declare @l int
set @l=0
insert @re select [pid],@l from [tb] where [id]=@id and [pid]<>0
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.[pid],@l
from [tb] a,@re b
where a.[id]=b.[id] and b.[level]=@l-1 and a.[pid]<>0
end
return
end
go
--调用(查询所有的父)
select a.* from [tb] a,f_pid(7)b where a.[id]=b.[id]
go
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_id]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_id]
GO
/*--树形数据处理
级别及排序字段
--邹建 2003-12(引用请保留此信息)--*/
/*--调用示例
--调用函数实现分级显示
select replicate('-',b.[level]*4)+a.name
from [tb] a,f_id()b
where a.[id]=b.[id]
order by b.sid
--*/
create function f_id()
returns @re table([id] int,[level] int,sid varchar(8000))
as
begin
declare @l int
set @l=0
insert @re select [id],@l,right(10000+[id],4)
from [tb] where [pid]=0
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.[id],@l,b.sid+right(10000+a.[id],4)
from [tb] a,@re b
where a.[pid]=b.[id] and b.[level]=@l-1
end
return
end
go
--调用函数实现分级显示
select replicate('-',b.[level]*4)+a.name
from [tb] a,f_id()b
where a.[id]=b.[id]
order by b.sid
go
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_copyid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_copyid]
GO
/*--树形数据处理
将一个结点下的所有子复制到另一个结点下
目标结点下不能有子结点
--邹建 2005-01(引用请保留此信息)--*/
/*--调用示例
--调用存储过程实现复制
exec p_copyid 2,10
select * from [tb]
--*/
create proc p_copyid
@s_id int,--要复制的源结点
@d_id int--目标结点
as
if not exists(select * from [tb] where [id]=@d_id)
begin
raiserror(N'目标结点 "%d" 不存在!',1,16,@d_id)
return
end
declare @s nvarchar(4000)
select @s=N'create table #t([nid] int identity('
+rtrim(max([id])+1)--复制后的结点为表中最大的结点+1
+',1),[id] int,[pid] int,level int,[name] nvarchar(50))
declare @l int
set @l=0
insert #t([id],[pid],level,[name]) select [id],@d_id,@l,[name]
from [tb] where [pid]=@s_id
while @@rowcount>0
begin
set @l=@l+1
insert #t([id],[pid],level,[name])
select a.[id],b.nid,@l,a.[name]
from [tb] a,#t b
where a.[pid]=b.[id]
and b.level=@l-1
end
set identity_insert [tb] on
insert [tb]([id],[pid],[name])
select nid,[pid],[name] from #t
set identity_insert [tb] off'
from [tb]
exec sp_executesql @s
,N'@s_id int,@d_id int'
,@s_id,@d_id
go
--删除测试
drop table [tb]
drop function f_cid
drop function f_pid
drop function f_id
drop proc p_copyid
go
- --树形数据查询示例
- 树形数据查询示例
- SqlServer 递归查询树形数据
- SqlServer 递归查询树形数据
- SqlServer 递归查询树形数据
- SqlServer 递归查询树形数据
- SqlServer 递归查询树形数据
- Mysql查询树形结构数据
- oracle 查询数据生成树形结构
- oracle查询树形结构数据的语句
- MySQL树形数据查询及展示
- oracle 递归 树形结构数据查询
- mysql使用函数查询树形数据
- mysql使用函数查询树形数据
- 黄金数据查询演示示例
- 黄金数据查询演示示例
- 黄金数据查询演示示例
- 黄金数据查询演示示例
- 如何通过VS2005创建虚拟目录
- linux编程_Makefile文件
- 今天破解了——FlyTreeView for Asp.net 3.6(破解下载)
- 彻底删除项目的VSS源代码管理信息 [转]
- SQL Server2000 公式字段的计算
- 树形数据查询示例
- NHibernate快速指南
- 补充一个关于COM+配置的Check List吧。[转]
- 从社会发展的眼光看编程
- javascript客户端实现导入到excel、word
- 获得连接池
- 2007年西医综合大纲考试形式和试卷结构
- 北京.NET俱乐部第十一次沙龙-“The 2007 Microsoft Office System-新的工作平台,新的开发平台”
- C++本质