SQL查询根节点

/*
标题:查询指定节点及其所有父节点的函数
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*/

create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values(‘001‘ , null  , ‘广东省‘)
insert into tb values(‘002‘ , ‘001‘ , ‘广州市‘)
insert into tb values(‘003‘ , ‘001‘ , ‘深圳市‘)
insert into tb values(‘004‘ , ‘002‘ , ‘天河区‘)
insert into tb values(‘005‘ , ‘003‘ , ‘罗湖区‘)
insert into tb values(‘006‘ , ‘003‘ , ‘福田区‘)
insert into tb values(‘007‘ , ‘003‘ , ‘宝安区‘)
insert into tb values(‘008‘ , ‘007‘ , ‘西乡镇‘)
insert into tb values(‘009‘ , ‘007‘ , ‘龙华镇‘)
insert into tb values(‘010‘ , ‘007‘ , ‘松岗镇‘)
go

--查询指定节点及其所有父节点的函数
create function f_pid(@id varchar(3)) returns @t_level table(id varchar(3))
as
begin
  insert into @t_level select @id
  select @id = pid from tb where id = @id and pid is not null
  while @@ROWCOUNT > 0
  begin
    insert into @t_level select @id select @id = pid from tb where id = @id and pid is not null
  end
  return
end
go

--调用函数查询002(广州市)及其所有父节点
select a.* from tb a , f_pid(‘002‘) b where a.id = b.id order by a.id
/*
id   pid  name
---- ---- ----------
001  NULL 广东省
002  001  广州市

(所影响的行数为 2 行)
*/

--调用函数查询003(深圳市)及其所有父节点
select a.* from tb a , f_pid(‘003‘) b where a.id = b.id order by a.id
/*
id   pid  name
---- ---- ----------
001  NULL 广东省
003  001  深圳市

(所影响的行数为 2 行)
*/

--调用函数查询008(西乡镇)及其所有父节点
select a.* from tb a , f_pid(‘008‘) b where a.id = b.id order by a.id
/*
id   pid  name
---- ---- ----------
001  NULL 广东省
003  001  深圳市
007  003  宝安区
008  007  西乡镇

(所影响的行数为 4 行)
*/

drop table tb
drop function f_pid

http://bbs.csdn.net/topics/300112071

时间: 2024-08-26 04:18:23

SQL查询根节点的相关文章

ORACLE 树形遍历查询根节点、父节点、子节点

1.准备演示数据 创建表结构: -- Create table createtable Z_ORG(  cid         NUMBER,  cname       VARCHAR2(32),  parent_id   NUMBER,  create_time DATE,  org_level   NUMBER) tablespace POWERDESK pctfree10 initrans1 maxtrans255; -- Add comments to the table comment

SQL查询指定节点及其所有父节点的方法

--查询ID = '009'的所有父节点 SET @ID = '009' ;WITH T AS ( SELECT ID , PID , NAME FROM TB WHERE ID = @ID UNION ALL SELECT A.ID , A.PID , A.NAME FROM TB AS A JOIN T AS B ON A.ID = B.PID ) SELECT * FROM T ORDER BY ID /* ID PID NAME ---- ---- ---------- 001 NULL

SQL查询父节点下的所有子节点(包括子节点下的子节点,无限子节点)

-->Title:Generating test data -->Author:wufeng4552 -->Date :2009-09-30 08:52:38 set nocount on if object_id('tb','U')is not null drop table tb go create table tb(ID int, ParentID int) insert into tb select 1,0 insert into tb select 2,1 insert int

(DS 《算法竞赛入门经典》)LA 3027 Corporative Network(查询某一个节点到根节点之间的距离)

题目大意: 查询某一个节点到根节点之间的距离 解题思路: 加权并查集问题.之前做的题目是"查看两个或多个节点是否在同一个集合下",现在的题目是"查询某个节点到 根节点之间的距离".之前只需要使用到father[x]这个数组,用来表示x的父亲节点是谁.现在引入dist[x]数组,用来记录 x节点到根节点的距离 1)在并查集中,根节点不懂,其他节点都可以动. A very big corporation is developing its corporative net

用DOM解析XML ,用xpath快速查询XML节点

XPath是一种快速查询xml节点和属性的一种语言,Xpath和xml的关系就像是sql语句和数据库的关系.用sql语句可以从数据库中快速查询出东西同样的用xPath也可以快速的从xml中查询出东西. 下面的示例演示了怎么用jdk自带的rt.jar完成dom解析 代码如下: test.xml的代码如下: <?xml version="1.0" encoding="UTF-8" ?> <inventory> <book year=&quo

【JSTREE】 jstree-初始化时默认选中根节点

var contacttree = $('#contact-org').jstree({ "core" : { "animation" : false, "multiple" : false, "check_callback" : true, "themes" : { "icons" : true, "dots" : false }, "data"

oracle新建用户执行sql查询语句出现错误ORA-00942:表或视图不存在

oracle创建新用户后客户端执行SQL查询后出现错误提示如下: 执行查询语句如下: select * from sm_sales_order; ORA-00942:表或视图不存在 创建新用户并指定表空间和临时表空间 CREATE USER xxx IDENTIFIED BY xxxx DEFAULT TABLESPACE LMS TEMPORARY TABLESPACE TEMP; 授予系统权限connect grant connect to xxx; 授予对象权限,只限查询 grant se

如何编写高效的SQL查询语句

概述 如何编写性能比较高的SQL查询语句呢?两个方法:创建合理的索引:书写高效的SQL语句 索引的基本原理 索引分为聚集索引和非聚集索引.一个表只能创建一个聚集索引和N个非聚集索引,这句话的由来主要是由于索引的原理决定的. 数据库中的一张表不论你创建不创建索引,或者,不论你创建那种类型的索引,其在硬盘上的存储是一样的,那么,创建索引和不创建索引,或者,创建聚集索引和非聚集索引的区别在什么地方呢? 其区别是表内数据在内存的存在形式.对于没有创建索引的表,其加载到内存里时,就只有数据块:对于有聚集索

(hdu step 5.1.5)Dragon Balls(求并查集的根节点、节点数和个结点的移动次数)

题目: Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 562 Accepted Submission(s): 239   Problem Description Five hundred years later, the number of dragon balls will increase unexpected