我们在工作中经常会遇到字典管理,字典管理的表Dictionary结构如下:SN(主键) FKSN(外键) ID(编号) Title(标题)
1 0 001 信息中心
2 0 002 业务中心
3 1 001 人员管理
4 1 002 产品管理
5 1 003 帐户管理
6 2 001 销售管理
7 2 002 撤单管理
8 2 003 退货管理
9 3 001 性别字典
10 6 001 结算字典
11 9 001 男
12 9 002 女
13 10 001 现金
14 10 002 欠款
这样就构造成了一棵以FKSN=0为根节点的树。
给定任何一个节点的SN 请查询出所有的子节点。(如果SN=13则应该查出“现金” 如果SN=10则查出“结算字典”“现金”和“欠款” 如SN=1 则查出“信息中心” “人员管理” “产品管理” “帐户管理” “性别字典” “男” “女”)
要求写一个接收SN为参数,返回所有子节点的存储过程。
1 0 001 信息中心
2 0 002 业务中心
3 1 001 人员管理
4 1 002 产品管理
5 1 003 帐户管理
6 2 001 销售管理
7 2 002 撤单管理
8 2 003 退货管理
9 3 001 性别字典
10 6 001 结算字典
11 9 001 男
12 9 002 女
13 10 001 现金
14 10 002 欠款
这样就构造成了一棵以FKSN=0为根节点的树。
给定任何一个节点的SN 请查询出所有的子节点。(如果SN=13则应该查出“现金” 如果SN=10则查出“结算字典”“现金”和“欠款” 如SN=1 则查出“信息中心” “人员管理” “产品管理” “帐户管理” “性别字典” “男” “女”)
要求写一个接收SN为参数,返回所有子节点的存储过程。
SN(主键) FKSN(外键) ID(编号) Title(标题)
1 0 001 信息中心
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_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @id , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.id , @level
from tb a , @t_Level b
where a.pid = b.id and b.level = @level - 1
end
return
end
go --调用函数查询001(广东省)及其所有子节点
select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
002 001 广州市
003 001 深圳市
004 002 天河区
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇 (所影响的行数为 10 行)
*/ --调用函数查询002(广州市)及其所有子节点
select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
002 001 广州市
004 002 天河区 (所影响的行数为 2 行)
*/ --调用函数查询003(深圳市)及其所有子节点
select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
003 001 深圳市
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇 (所影响的行数为 7 行)
*/ drop table tb
drop function f_cid本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fredrickhu/archive/2009/09/20/4573382.aspx
GO-- 建立演示环境
CREATE TABLE Dept(
id int PRIMARY KEY,
parent_id int,
name nvarchar(20))
INSERT Dept
SELECT 0, 0, N'<全部>' UNION ALL
SELECT 1, 0, N'财务部' UNION ALL
SELECT 2, 0, N'行政部' UNION ALL
SELECT 3, 0, N'业务部' UNION ALL
SELECT 4, 0, N'业务部' UNION ALL
SELECT 5, 4, N'销售部' UNION ALL
SELECT 6, 4, N'MIS' UNION ALL
SELECT 7, 6, N'UI' UNION ALL
SELECT 8, 6, N'软件开发' UNION ALL
SELECT 9, 8, N'内部开发'
GO-- 查询指定部门下面的所有部门
DECLARE @Dept_name nvarchar(20)
SET @Dept_name = N'MIS'
;WITH
DEPTS AS(
-- 定位点成员
SELECT * FROM Dept
WHERE name = @Dept_name
UNION ALL
-- 递归成员, 通过引用CTE自身与Dept基表JOIN实现递归
SELECT A.*
FROM Dept A, DEPTS B
WHERE A.parent_id = B.id
)
SELECT * FROM DEPTS
GO
/*id parent_id name
----------- ----------- --------------------
6 4 MIS
7 6 UI
8 6 软件开发
9 8 内部开发(4 行受影响)*/-- 删除演示环境
DROP TABLE Dept
-- Author: T.O.P
-- Create date: 2009/11/27
-- Version: SQL SERVER 2005
-- =============================================
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([SN] int,[FKSN] int,[ID] varchar(3),[Title] varchar(8))
insert [tb]
select 1,0,'001','信息中心' union all
select 2,0,'002','业务中心' union all
select 3,1,'001','人员管理' union all
select 4,1,'002','产品管理' union all
select 5,1,'003','帐户管理' union all
select 6,2,'001','销售管理' union all
select 7,2,'002','撤单管理' union all
select 8,2,'003','退货管理' union all
select 9,3,'001','性别字典' union all
select 10,6,'001','结算字典' union all
select 11,9,'001','男' union all
select 12,9,'002','女' union all
select 13,10,'001','现金' union all
select 14,10,'002','欠款'
goCREATE PROCEDURE proc_1
(
@sn int
)
as
with cte as
(
select * from tb where sn=@sn
union all
select a.*
from tb a inner join cte c on a.[FKSN] = c.[SN]
)
select * from cte
goexec proc_1 1drop table tb
drop procedure proc_1
--测试结果:
/*
SN FKSN ID Title
----------- ----------- ---- --------
1 0 001 信息中心
3 1 001 人员管理
4 1 002 产品管理
5 1 003 帐户管理
9 3 001 性别字典
11 9 001 男
12 9 002 女(7 row(s) affected)*/
if object_id('tb') is not null drop table tb
go
create table tb (SN int,FKSN int,ID varchar(3),Title varchar(8))
insert into tb
select 1,0,'001','信息中心' union all
select 2,0,'002','业务中心' union all
select 3,1,'001','人员管理' union all
select 4,1,'002','产品管理' union all
select 5,1,'003','帐户管理' union all
select 6,2,'001','销售管理' union all
select 7,2,'002','撤单管理' union all
select 8,2,'003','退货管理' union all
select 9,3,'001','性别字典' union all
select 10,6,'001','结算字典' union all
select 11,9,'001','男' union all
select 12,9,'002','女' union all
select 13,10,'001','现金' union all
select 14,10,'002','欠款'-------------------------------------------
---->SQL 2005
if object_id('p_bom') is not null drop proc p_bom
go
create proc p_bom
@sn int
as;with szy as
(
select sn,fksn,title from tb where sn=@sn
union all
select a.sn,a.fksn,a.title from tb a,szy b
where a.fksn=b.sn
)
select * from szygo ------------------------------->
exec p_bom 10sn fksn title
----------- ----------- --------
10 6 结算字典
13 10 现金
14 10 欠款(3 行受影响)
-- Author: T.O.P
-- Create date: 2009/11/27
-- Version: SQL SERVER 2005
-- =============================================
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([SN] int,[FKSN] int,[ID] varchar(3),[Title] varchar(8))
insert [tb]
select 1,0,'001','信息中心' union all
select 2,0,'002','业务中心' union all
select 3,1,'001','人员管理' union all
select 4,1,'002','产品管理' union all
select 5,1,'003','帐户管理' union all
select 6,2,'001','销售管理' union all
select 7,2,'002','撤单管理' union all
select 8,2,'003','退货管理' union all
select 9,3,'001','性别字典' union all
select 10,6,'001','结算字典' union all
select 11,9,'001','男' union all
select 12,9,'002','女' union all
select 13,10,'001','现金' union all
select 14,10,'002','欠款'
goCREATE PROCEDURE proc_1
(
@sn int
)
as
with cte as
(
select * from tb where sn=@sn
union all
select a.*
from tb a inner join cte c on a.[FKSN] = c.[SN]
)
select * from cte
goexec proc_1 1
exec proc_1 10
exec proc_1 13drop table tb
drop procedure proc_1
--测试结果:
/*
--SN=1
SN FKSN ID Title
----------- ----------- ---- --------
1 0 001 信息中心
3 1 001 人员管理
4 1 002 产品管理
5 1 003 帐户管理
9 3 001 性别字典
11 9 001 男
12 9 002 女(7 row(s) affected)--SN=10
SN FKSN ID Title
----------- ----------- ---- --------
10 6 001 结算字典
13 10 001 现金
14 10 002 欠款(3 row(s) affected)--SN=13
SN FKSN ID Title
----------- ----------- ---- --------
13 10 001 现金(1 row(s) affected)
*/
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-11-27 13:36:03
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([SN] int,[FKSN] int,[ID] varchar(3),[Title] varchar(8))
insert [tb]
select 1,0,'001','信息中心' union all
select 2,0,'002','业务中心' union all
select 3,1,'001','人员管理' union all
select 4,1,'002','产品管理' union all
select 5,1,'003','帐户管理' union all
select 6,2,'001','销售管理' union all
select 7,2,'002','撤单管理' union all
select 8,2,'003','退货管理' union all
select 9,3,'001','性别字典' union all
select 10,6,'001','结算字典' union all
select 11,9,'001','男' union all
select 12,9,'002','女' union all
select 13,10,'001','现金' union all
select 14,10,'002','欠款'
--------------开始查询--------------------------
DECLARE @sn int
SET @sn = 1
;WITH
f AS(
-- 定位点成员
SELECT * FROM tb
WHERE sn = @sn
UNION ALL
SELECT A.*
FROM tb A, f B
WHERE A.[FKSN] = B.[SN]
)
SELECT * FROM f
GO----------------结果----------------------------
/*SN FKSN ID Title
----------- ----------- ---- --------
1 0 001 信息中心
3 1 001 人员管理
4 1 002 产品管理
5 1 003 帐户管理
9 3 001 性别字典
11 9 001 男
12 9 002 女(7 行受影响)
*/
--SQL2000的解决办法, 用了函数-- =============================================
-- Author: T.O.P
-- Create date: 2009/11/27
-- Version: SQL SERVER 2005
-- =============================================IF OBJECT_ID('dbo.fn_subordinates1') IS NOT NULL
DROP FUNCTION dbo.fn_subordinates1;
GO
CREATE FUNCTION dbo.fn_subordinates1(@root AS INT)
RETURNS @Subs TABLE
(
SN INT NOT NULL PRIMARY KEY NONCLUSTERED,
FKSN INT NOT NULL,
ID varchar(3),
TITLE varchar(8),
lvl INT NOT NULL
)
AS
begin
declare @lv int
set @lv=0
insert @Subs
select *, @lv from tb where SN = @root
while @@rowcount>0
begin
set @lv=@Lv+1;
insert @subs
select b.* ,@Lv
from @subs a join tb b on a.sn=b.FKSN and lvl=@lv-1
end
return;
end
goif object_id('[tb]') is not null drop table [tb]
go
create table [tb]([SN] int,[FKSN] int,[ID] varchar(3),[Title] varchar(8))
insert [tb]
select 1,0,'001','信息中心' union all
select 2,0,'002','业务中心' union all
select 3,1,'001','人员管理' union all
select 4,1,'002','产品管理' union all
select 5,1,'003','帐户管理' union all
select 6,2,'001','销售管理' union all
select 7,2,'002','撤单管理' union all
select 8,2,'003','退货管理' union all
select 9,3,'001','性别字典' union all
select 10,6,'001','结算字典' union all
select 11,9,'001','男' union all
select 12,9,'002','女' union all
select 13,10,'001','现金' union all
select 14,10,'002','欠款'
goCREATE PROCEDURE proc_1
(
@sn int
)
as SELECT * FROM dbo.fn_subordinates1(@sn) AS S;
goexec proc_1 1
exec proc_1 10
exec proc_1 13drop table tb
drop procedure proc_1
drop function fn_subordinates1
--测试结果:
/*
--SN=1
SN FKSN ID TITLE lvl
----------- ----------- ---- -------- -----------
1 0 001 信息中心 0
3 1 001 人员管理 1
4 1 002 产品管理 1
5 1 003 帐户管理 1
9 3 001 性别字典 2
11 9 001 男 3
12 9 002 女 3--SN=10SN FKSN ID TITLE lvl
----------- ----------- ---- -------- -----------
10 6 001 结算字典 0
13 10 001 现金 1
14 10 002 欠款 1(3 row(s) affected)--SN=13SN FKSN ID TITLE lvl
----------- ----------- ---- -------- -----------
13 10 001 现金 0*/
go
insert into Dictionary select
1,0,'001','信息中心' union all select
2,0,'002','业务中心' union all select
3,1,'001','人员管理' union all select
4,1,'002','产品管理' union all select
5,1,'003','帐户管理' union all select
6,2,'001','销售管理' union all select
7,2,'002','撤单管理' union all select
8,2,'003','退货管理' union all select
9,3,'001','性别字典' union all select
10,6,'001','结算字典' union all select
11,9,'001','男' union all select
12,9,'002','女' union all select
13,10,'001','现金' union all select
14,10,'002','欠款'
go--drop proc getSubNode
--go
create proc getSubNode(@id int)
as
begin
;with cte
as
(
select * from Dictionary where SN=@id
union all
select D.* from Dictionary D
inner join cte t On D.FKSN = t.SN
)
select * from cte option(maxrecursion 0)
endexec getsubNode 1
/*SN FKSN ID Title
----------- ----------- ---------- ----------
1 0 001 信息中心
3 1 001 人员管理
4 1 002 产品管理
5 1 003 帐户管理
9 3 001 性别字典
11 9 001 男
12 9 002 女(7 行受影响)*/