sql語句遞迴查詢

2021-09-29 02:49:55 字數 3165 閱讀 6039

表結構: 

給出乙個結點找到該節點的所有  子  節點:

with c_depts as

( select dept.* from department dept where dept.pptr='父節點id'

union all

select dept.* from c_depts ,department dept where dept.pptr=c_depts .deptid

)select c_depts .deptid from c_depts

給出乙個結點找到該節點的所有  父  節點:

with p_depts as

( select dept.* from department dept where dept.deptid='結點id'

union all

select dept.* from p_depts ,department dept where dept.deptid=p_depts .pptr

)select p_depts .deptid from p_depts

也可以在  with 後跟其他語句,比如我要實現的功能是將deptid=「001001」及下級單位刪除。(即將deptid=「001001」及子節點全部刪除)

with c_depts as

( select dept.* from department dept where dept.pptr='001001'

union all

select dept.* from c_depts ,department dept where dept.pptr=c_depts .deptid

)delete department where deptid

in (select c_depts .deptid from c_depts )

附建庫和生成資料的語句:

/******建庫******/

set ansi_nulls on

goset quoted_identifier on

goset ansi_padding on

gocreate table [dbo].[deptment](

[deptid] [varchar](50) not null,

[deptname] [nvarchar](200) null,

[pptr] [varchar](50) null,

constraint [pk_deptment] primary key clustered

( [deptid] asc

)with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary]

) on [primary]

goset ansi_padding off

go/*********插入資料***********/

insert [dbo].[deptment] ([deptid], [deptname], [pptr]) values (n'001', n'部門列表', null)

insert [dbo].[deptment] ([deptid], [deptname], [pptr]) values (n'001001', n'第一大部門', n'001')

insert [dbo].[deptment] ([deptid], [deptname], [pptr]) values (n'001002', n'第二大部門', n'001')

insert [dbo].[deptment] ([deptid], [deptname], [pptr]) values (n'2f09ed6b90074ce99339f5615a075529', null, n'a3e46530-cb26-4879-948e-09233ec4678b')

insert [dbo].[deptment] ([deptid], [deptname], [pptr]) values (n'37dc11eb-74a0-44e0-ab8b-6ae2e502e249', n'綜合部', n'001001')

insert [dbo].[deptment] ([deptid], [deptname], [pptr]) values (n'5193021e-d358-43d8-bfb9-aa042f82dcfc', n'綜合部1', n'37dc11eb-74a0-44e0-ab8b-6ae2e502e249')

insert [dbo].[deptment] ([deptid], [deptname], [pptr]) values (n'553274d8-2d86-46cd-8677-9e9190eda125', n'綜合部2', n'001001')

insert [dbo].[deptment] ([deptid], [deptname], [pptr]) values (n'6cb65a7f-128b-4fe5-92f7-f11e2aff28b4', n'發展部', n'5193021e-d358-43d8-bfb9-aa042f82dcfc')

insert [dbo].[deptment] ([deptid], [deptname], [pptr]) values (n'7a39c46d-c405-4ad0-b703-ef42884f4071', null, n'001002')

insert [dbo].[deptment] ([deptid], [deptname], [pptr]) values (n'a3e46530-cb26-4879-948e-09233ec4678b', n'技術部', n'001002')

insert [dbo].[deptment] ([deptid], [deptname], [pptr]) values (n'e6e46430-cb20-4809-949e-09233ec408b1', n'領導班子', n'37dc11eb-74a0-44e0-ab8b-6ae2e502e249')

SQL語句遞迴查詢

通過sql語句遞迴查詢所有下級或上級使用者 1.ms sql with cte as select id,pid,deptname,0as lvl from department where id 2union allselect d.id,d.pid,d.deptname,lvl 1 from c...

SQL語句遞迴查詢

通過sql語句遞迴查詢所有下級或上級使用者 1.ms sql with cte as select id,pid,deptname,0as lvl from department where id 2union allselect d.id,d.pid,d.deptname,lvl 1 from c...

sql語句遞迴查詢(start with)

在做專案中遇到乙個問題,就是同乙個表中的資料存在級聯關係,但是只要查出來的末級資料,糾結了好久,好不容易找到了乙個博主的分享,在這裡做個記錄,也是和大家一起分享學習一下這位大神的技術,共勉 寫 時碰到要弄清楚oracle的role之間的傳遞關係,就是有role a的話,可以通過grant a to ...