树形数据查询示例

--树形数据查询示例
--作者: 邹建

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

转载于:https://www.cnblogs.com/wgx1323/archive/2007/06/12/780274.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/381742.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

UVA 10588—— Queuing at the doctors

题意&#xff1a;给定n个工人和m个医生&#xff0c;然后进行体检&#xff0c;每个医生每秒接待一个工人&#xff0c;每个人都有一个体检项目顺序和时间&#xff0c;问最后一个员工完成体检的时间。 思路&#xff1a;优先队列模拟&#xff0c;建立m个项目的优先队列&#xff0c;…

[导入]javascript总结

1.动态添加一行&#xff0c;和删除当前行<script> var count0; function ff() { var txt1document.getElementById("Text1"); var table1document.getElementById("table1"); rowNotable1.rows.length; Trtable1.insertRow(rowNo); Tr.id"tr&qu…

UVA 10410——Tree Reconstruction

题意&#xff1a;给定一颗树的BFS和DFS&#xff0c;求这棵的每个节点。 思路&#xff1a;用栈模拟维护。对应的BFS为每个节点到根节点的距离&#xff0c;然后比较当前节点和栈顶节点与根的距离&#xff0c;如果当前节点大&#xff0c;则为栈顶节点的孩子&#xff0c;否则弹出继…

求助:DataGrid加行号的问题

我的数据是fname,lnameprivatevoidPage_Load(objectsender, System.EventArgs e) { if(!IsPostBack) { myConnectionnew SqlConnection("server127.0.0.1;uidsa;pwdsa;databaseqqq;"); strSQL"SELE…

UVA 10895——Matrix Transpose

题意&#xff1a;给定一个矩阵&#xff08;每一行有几个非0的数据&#xff0c;对应的位置以及数值&#xff09;&#xff0c;输出这个矩阵的转置矩阵。 思路&#xff1a;直接模拟&#xff0c;用两个vector&#xff0c;一个维护数值&#xff0c;另外一个维护行号。注意长度为0时输…

UVA 514——Rails

题意&#xff1a;给定两个序列A和一到n的排列B&#xff0c;问能否通过一个栈的push和pop操作使得A变成B。 思路&#xff1a;直接构造一个栈模拟即可&#xff0c;注意换行。 code&#xff1a; #include <bits/stdc.h> using namespace std;int v[1005],n;int main() {whi…

UVA 536——Tree Recovery

题意&#xff1a;给定一颗树的先根遍历和中根遍历&#xff0c;然后求后根遍历。 思路&#xff1a;先根遍历的第一个为root&#xff0c;然后找到root在中根的位置&#xff0c;进而递归左右儿子求解。 code&#xff1a; #include <iostream> #include <cstdio> #incl…

CVS的使用教程(转)

、什么是CVS? CVS - Concurrent Versions System&#xff08;并发版本管理系统&#xff09;是一个版本控制管理系统&#xff0c;它是目前最为广泛使用的一个系统。 在多人共同开发一个大型项目时&#xff0c;源代码的维护和版本维护是一件令人头疼的事情&#xff0c;由于多人开…

6.22打包建立ISS虚拟目录,安装完运行你想运行的程序

http://installshield.jaron.cn/forum/dispbbs.asp?boardID3&ID284614&page1#include "ifx.h" #define Emty "" //宏定义DOS功能把Emty 替换为"" prototype RegUnInstall(STRING);string szDir, szVirtual;prototype void CheckReq…

UVA 11991——Easy Problem from Rujia Liu?

题意&#xff1a;给定一个数组&#xff0c;然后有若干组询问&#xff0c;每次询问求第k个v出现的位置。 思路&#xff1a;用vector构造模拟&#xff0c;吧相同的数的位置放在同一个vector里&#xff0c;对于每次查询输出mp[v][x-1]; code&#xff1a; #include <bits/stdc.h…

压缩图片上传到数据库

保存到数据库public int DyfcListInsert(int id,string name,string username,string content,Byte[] photo) { string sql "S_DyfcList_Insert"; SqlCommand sqlcmd new SqlCommand(sql,DwzxConfiguration.ConnectDB() ,DwzxConfigu…

支持.NET的分布式缓存系统memcached

http://www.infoq.com/news/2007/07/memcached 转载于:https://www.cnblogs.com/didasoft/archive/2007/07/17/821766.html

UVA 1160——X-Plosives

题意&#xff1a;给定一些化合物&#xff08;含有两个元素&#xff09;&#xff0c;当满足k个化合物且有k个元素的时候会发生爆炸&#xff0c;问多少个化合物是不能装车的。 思路&#xff1a;并查集的简单应用。实际上满足条件的时候是一个环&#xff0c;因此用并查集简单判环即…

UVA 1329——Corporative Network

题意&#xff1a;有n个节点&#xff0c;然后执行I u&#xff0c;v&#xff08;把u的父节点设为v&#xff09;和E u&#xff08;询问u到根节点的距离&#xff09;。 思路&#xff1a;并查集。加了信息的并查集&#xff0c;在路径压缩的同时维护距离d[i]; code&#xff1a; #inc…

Atlas 不仅仅是异步

最近学习研究了一下微软的AJAX框架,Atlas.这个框架对于实现AJAX里的异步请求,无刷新等技术非常的简便,功能也很强大,当然这些都是建立在DOTNET平台上. 对于这个框架,给我印象很深的就是,作为一个新的框架,能够与现有的ASP.NET技术实现几乎无缝的整合,并且只需要添加若干行…

UVA 11988——Broken Keyboard (a.k.a. Beiju Text)

题意&#xff1a;给定一个字符串&#xff0c;然后【会将光标跳转到头&#xff0c;】会将光标调到尾&#xff0c;问最后正确的输入。 思路&#xff1a;直接用list来模拟即可&#xff0c;【的时候就在头插&#xff0c;】就在尾插&#xff0c;也可根据递归顺序解。 code&#xff1…

使用CodeDom生成程序集

usingSystem;usingMicrosoft.CSharp;usingSystem.CodeDom.Compiler;usingSystem.CodeDom;namespaceTest.CUI{ class Program { static void Main() { // 创建编译器对象 CSharpCodeProvider p new CSharpCodeProvider(); ICodeCompiler cc p.CreateCo…

UVA 11136——Hoax or what

题意&#xff1a;超市搞促销&#xff0c;每天都从箱子里拿出最大和最小的差作为促销金额&#xff0c;给出n天的促销情况&#xff0c;问最后总的促销金额。 思路&#xff1a;set构造&#xff0c;当有小票的时候放入set&#xff0c;每天结束的时候取出头和尾即可。 code&#xff…

SQL Server与Oracle、DB2三种数据库比较

开发数据库应用&#xff0c;选择一个好的数据库是非常重要的。本文从一些方面比较了SQL Server与Oracle、DB2三种数据库&#xff0c;为你选择数据库提供一些参考。开放性 SQL Server只能在Windows 上运行&#xff0c;没有丝毫的开放性&#xff0c;操作系统的系统的稳定对数据库…

汇编语言——第1次上机实验

准备&#xff1a; 硬件&#xff1a;pc机&#xff0c;32位win操作系统&#xff0c;能够运行dos&#xff0c;某些64位win10系统可能会不支持 软件&#xff1a;masm程序 实验内容&#xff1a; 1.winr运行dos&#xff0c;cd到指定的masm目录&#xff08;为了调试方便&#xff0c;所…