SqlServer 中的递归查询
use test set nocount on if object_id ( ' Dept ' , ' U ' ) is not null drop table Dept go create table Dept(ID int ,ParentID int ,Name varchar ( 20 )) insert into Dept select 1 , 0 , ' AA ' insert into Dept select 2 , 1 , ' BB ' insert into
use test
set nocount on
if object_id('Dept','U') isnotnull
drop table Dept
go
create table Dept(ID int,ParentID int,Name varchar(20))
insert into Dept select 1,0,'AA'
insert into Dept select 2,1,'BB'
insert into Dept select 3,1,'CC'
insert into Dept select 4,2,'DD'
insert into Dept select 5,3,'EE'
insert into Dept select 6,0,'FF'
insert into Dept select 7,6,'GG'
insert into Dept select 8,7,'HH'
insert into Dept select 9,7,'II'
insert into Dept select 10,7,'JJ'
insert into Dept select 11,9,'KK'
go
SELECT*FROM Dept;
--查询树状结构某节点的上级所有根节点。
with cte_root(ID,ParentID,NAME)
as
(
--起始条件
select ID,ParentID,NAME from Dept where ID='1'--查询条件
union all
--递归条件
select a.ID,a.ParentID,a.NAME from Dept a
innerjoin cte_root b --执行递归,这里就要理解下了
on a.ID=b.ParentID --根据基础表条件查询子节点(a.ID),通过CTE递归找到其父节点(b.ParentID)。
) --可以和下面查询子节点的cte_child对比。
select*from cte_root ;
--查询树状结构某节点下的所有子节点。
with cte_child(ID,ParentID,NAME)
as
(
--起始条件
select ID,ParentID,NAME
from Dept
where Name ='II'--列出父节点查询条件
union all
--递归条件
select a.ID,a.ParentID,a.NAME
from Dept a
inner join
cte_child b
on a.ID=b.ParentID --根据查询到的父节点(a.Parent),通过CTE递归查询出其子节点(b.ID)
)
select*from cte_child --可以改变之前的查询条件'II'再测试结果
ID ParentID Name
----------- ----------- --------------------
10 AA
21 BB
31 CC
42 DD
53 EE
60 FF
76 GG
87 HH
97 II
107 JJ
119 KK
ID ParentID NAME
----------- ----------- --------------------
97 II
76 GG
60 FF
ID ParentID NAME
----------- ----------- --------------------
97 II
119 KK

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Huawei is rolling out software version 5.0.0.100(C00M01) for the Watch GT 5 and the Watch GT 5 Prosmartwatchesglobally. These two smartwatches recently launched in Europe, with the standard model arriving as the company’s cheapest model. This Harmony

Katsuhiro Harada, the Tekken series director, once seriously tried to bring Colonel Sanders into the iconic fighting game. In an interview with TheGamer, Harada revealed that he pitched the idea to KFC Japan, hoping to add the fast-food legend as a g

Tesla is rolling out the latest Full Self-Driving (Supervised) version 12.5.5 and with it comes the promised Cybertruck FSD option at long last, ten months after the pickup went on sale with the feature included in the Foundation Series trim price. F

Garmin is ending the month with a new set of stable updates for its latest high-end smartwatches. To recap, the company released System Software 11.64 to combat high battery drain across the Enduro 3, Fenix E and Fenix 8 (curr. $1,099.99 on Amazon).

Xiaomi will shortly launch the Mijia Graphene Oil Heater in China. The company recently ran a successful crowdfunding campaign for the smart home product, hosted on its Youpin platform. According to the page, the device has already started to ship to

Earlier in September 2024, Anker's Zolo 140W charger was leaked, and it was a big deal since it was the first-ever wall charger with a display from the company. Now, a new unboxing video from Xiao Li TV on YouTube gives us a first-hand look at the hi

The launch of Samsung's long-awaited 'Special Edition' foldable has taken another twist. In recent weeks, rumours about the so-called Galaxy Z Fold Special Edition went rather quiet. Instead, the focus has shifted to the Galaxy S25 series, including

With a history of over one decade, Manjaro is regarded as one of the most user-friendly Linux distros suitable for both beginners and power users, being easy to install and use. Mostly developed in Austria, Germany, and France, this Arch-based distro
