B+树查询上下级(sql实现)

简介:
+关注继续查看
     B+树查询上下级(sql实现)
 
 
use pubs
 
--drop table employee
 
create table employee
(
 empId char(32) primary key,
 empName varchar(20),
 higherUpId char(32)
)
 
insert into employee values('0001','aa',null)
 
insert into employee values('0002','bb','0001')
insert into employee values('0003','cc','0001')
 
insert into employee values('0004','dd','0002')
insert into employee values('0005','ee','0002')
 
insert into employee values('0006','ff','0003')
insert into employee values('0007','gg','0003')
 
insert into employee values('0008','hh','0004')
insert into employee values('0009','ii','0004')
 
insert into employee values('0010','jj','0007')
insert into employee values('0011','kk','0007')
 
 
 
--查下级和间接下级
create proc proc_treeDownQuery
 @id varchar(20)
as
 declare @temp varchar(2000)
 declare @tempCount nvarchar(2000)
 declare @sql varchar(5000)
 declare @count int
 
 set @sql = 'select empId from employee where higherUpId = ' + @id
 set @temp = 'select empId from employee where higherUpId = '+ @id
 
 while (1=1)
 begin
  set @tempCount = 'select @count=count(*) from employee where higherUpId in (' + @temp + ')'
  exec sp_executesql @tempCount,N'@count int output',@count output
 
  if (@count=0)
   begin
    break
   end
  else
   begin
    set @temp = 'select empId from employee where higherUpId in (' + @temp + ')'
    set @sql = @sql +  ' union ' + @temp
   end
  
 end
 
 exec(@sql)
 
 
go
 
exec proc_treeDownQuery '0001'
 

--drop proc proc_treeUpQuery
 

--查上级和间接上级
create proc proc_treeUpQuery
 @id varchar(20)
as
 declare @count int
 declare @sql varchar(5000)
 declare @temp varchar(2000)
 declare @tempCount nvarchar(2000)
 
 set @sql = 'select higherUpId from employee where empId = ' + @id
 set @temp = 'select higherUpId from employee where empId = ' + @id
 
 while (1=1)
 begin 
  set @tempCount = 'select @count=count(higherUpId) from employee where empId in (' + @temp + ')'
  exec sp_executesql @tempCount,N'@count int output',@count output
 
  if (@count=0)
   begin
    break
   end
  else
   begin
    set @temp = 'select higherUpId from employee where empId in (' + @temp + ')'
    set @sql = @sql + ' union ' + @temp
   end
 end
 
 exec(@sql)
go
 

exec proc_treeUpQuery '0009'
 
 
 

 
版权说明

  如果标题未标有<转载、转>等字则属于作者原创,欢迎转载,其版权归作者和博客园共有。
  作      者:温景良
  文章出处:http://wenjl520.cnblogs.com/  或  http://www.cnblogs.com/

posted @ 2009-04-28 23:49 温景良(Jason) Views(1624) Comments(0) Edit 收藏
 

公告

hidden hit counter \\\
 
本文转自我的程序人生博客园博客,原文链接:http://www.cnblogs.com/wenjl520/archive/2009/04/28/1445778.html,如需转载请自行联系原作者
 
相关文章
|
16天前
|
SQL
在sql 里嵌套查询时,查询条件带有instr时报错
在sql 里嵌套查询时,查询条件带有instr时报错
15 1
|
18天前
|
SQL 存储 缓存
Flink CDC中flink sql 如果缓存起来所有的数据,然后基于这个数据做查询?
Flink CDC中flink sql 如果缓存起来所有的数据,然后基于这个数据做查询?
16 1
|
19天前
|
SQL 数据挖掘 关系型数据库
数据分析法宝,一个SQL语句查询多个异构数据源
NineData DSQL 是针对多个同异构数据库系统进行跨库查询的功能,当前支持对表和视图进行 SELECT 操作。您可以在一个查询中访问多个数据库,获取分散在各个数据库中的有用信息,并且将这些信息聚合为一份查询结果返回,轻松实现跨多个库、多个数据源,乃至跨多个异构数据源的数据查询。
234 0
数据分析法宝,一个SQL语句查询多个异构数据源
|
22天前
|
SQL 分布式计算 调度
在MaxCompute中,你可以通过SQL语句来查询和导出实例的运行状态和时间等信息
在MaxCompute中,你可以通过SQL语句来查询和导出实例的运行状态和时间等信息
25 5
|
23天前
|
SQL 存储 程序员
数据库SQL查询知识学习指导
数据库SQL查询知识学习指导
20 1
|
1月前
|
SQL 分布式计算 Java
201 Spark SQL查询程序
201 Spark SQL查询程序
17 0
|
2月前
|
SQL 数据库
达梦(DM) SQL查询及联合查询
继续讲解达梦(DM)数据库SQL查询操作
|
2月前
|
SQL 存储 分布式计算
HA3 SQL样本实验:一种混合计算查询的全新样本解决方案
HA3(对外开源代号:Havenask )是阿里智能引擎团队自研的大规模分布式检索系统,广泛应用于阿里内部的搜索业务,是十多年来阿里在电商领域积累下来的核心竞争力产品。Ha3 SQL 是在原有Ha3引擎基础上,新增的SQL查询功能,引擎内置了SQL形式的的查询语法,允许用户通过写SQL语句来构造引擎查询。
|
2月前
|
SQL 数据库
使用 SQL 进行排序查询
在数据库中,我们经常需要对查询的结果进行排序,以便更容易地理解和分析数据。SQL(Structured Query Language)提供了强大的排序功能,允许我们按照指定的列对数据进行升序或降序排序。本文将详细介绍如何使用 SQL 进行排序查询,包括基本的排序语法、多列排序、自定义排序顺序等内容。
65 0
|
3月前
|
SQL 算法 数据库
OBCP第三章、SQL引擎高级技术-查询改写
OBCP第三章、SQL引擎高级技术-查询改写
38 0
推荐文章
更多