现在有一张部门表(SYS_DEPT)结构如下:
DEPT_ID(部门ID) DEPT_NAME(部门名称) DEPT_PARENT_ID(部门父节点名称)要实现的内容为:
根据上面这张部门表中的任意一个DEPT_ID(部门ID)查询出该部门以及该部门下所有的部门信息。
如:
1 部门名称1 0
2 部门名称2 1
3 部门名称3 1
4 部门名称4 3
现在要根据“部门名称1”的ID查询出下面所有的部门信息。即:查询结果为上面所有部门信息。
在ORACLE中直接用 SELECT * FROM DEPT START WITH DEPT_ID=1 CONNECT BY PRIOR DEPT_ID=DEPT_PARENT_ID就可以做到了。
但在SQLSERVER中小弟不知道如何实现。
请各位大虾给点意见。好像是要写个存储过程。希望各位大虾给点支持!感谢!
DEPT_ID(部门ID) DEPT_NAME(部门名称) DEPT_PARENT_ID(部门父节点名称)要实现的内容为:
根据上面这张部门表中的任意一个DEPT_ID(部门ID)查询出该部门以及该部门下所有的部门信息。
如:
1 部门名称1 0
2 部门名称2 1
3 部门名称3 1
4 部门名称4 3
现在要根据“部门名称1”的ID查询出下面所有的部门信息。即:查询结果为上面所有部门信息。
在ORACLE中直接用 SELECT * FROM DEPT START WITH DEPT_ID=1 CONNECT BY PRIOR DEPT_ID=DEPT_PARENT_ID就可以做到了。
但在SQLSERVER中小弟不知道如何实现。
请各位大虾给点意见。好像是要写个存储过程。希望各位大虾给点支持!感谢!
create table BOM(ID INT,PID INT,MSG VARCHAR(1000))
insert into BOM select 1,0,NULL
insert into BOM select 2,1,NULL
insert into BOM select 3,1,NULL
insert into BOM select 4,2,NULL
insert into BOM select 5,3,NULL
insert into BOM select 6,5,NULL
insert into BOM select 7,6,NULL
go--创建用户定义函数
create function f_getChild(@ID VARCHAR(10))
returns @t table(ID VARCHAR(10),PID VARCHAR(10),Level INT)
as
begin
declare @i int,@ret varchar(8000)
set @i = 1
insert into @t select ID,PID,@i from BOM where PID = @ID
while @@rowcount<>0
begin
set @i = @i + 1
insert into @t
select
a.ID,a.PID,@i
from
BOM a,@t b
where
a.PID=b.ID and b.Level = @i-1
end
return
end
go--执行查询
select ID from dbo.f_getChild(3)
go--输出结果
/*
5
6
7
*/--删除测试数据
drop function f_getChild
drop table BOM
USE tempdb
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-- 删除演示环境
DROP TABLE Dept本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fredrickhu/archive/2009/09/19/4569529.aspx
--2000用函数
----------------------------------------------------------------------------------------------------------------
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_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
Create table SYS_DEPT(DEPT_ID int,DEPT_NAME varchar(10),DEPT_PARENT_ID int)
insert into SYS_DEPT select 1,'部门名称1',0
insert into SYS_DEPT select 2,'部门名称2',1
insert into SYS_DEPT select 3,'部门名称3',1
insert into SYS_DEPT select 4,'部门名称4',3
insert into SYS_DEPT select 5,'部门名称5',0
go--创建用户定义函数
create function f_getChild(@DEPT_ID VARCHAR(10))
returns @t table(DEPT_ID int,DEPT_NAME varchar(10),DEPT_PARENT_ID int,Level INT)
as
begin
declare @i int,@ret varchar(8000)
set @i = 1
insert into @t select *,@i from SYS_DEPT where DEPT_ID = @DEPT_ID
while @@rowcount<>0
begin
set @i = @i + 1
insert into @t
select
a.*,@i
from
SYS_DEPT a,@t b
where
a.DEPT_PARENT_ID=b.DEPT_ID and b.Level = @i-1
end
return
end
go--执行查询
select DEPT_ID,DEPT_NAME,DEPT_PARENT_ID from dbo.f_getChild(1) order by level,DEPT_ID
go--输出结果
/*
DEPT_ID DEPT_NAME DEPT_PARENT_ID
----------- ---------- --------------
1 部门名称1 0
2 部门名称2 1
3 部门名称3 1
4 部门名称4 3
*/--删除测试数据
drop function f_getChild
drop table SYS_DEPT
http://hi.csdn.net/link.php?url=http://blog.csdn.net%2Ffeixianxxx
-- Author :fredrickhu(小F,向高手学习)
-- Date :2009-11-03 23:07:55
-- 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.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([DEPT_ID] int,[DEPT_NAME] varchar(9),[DEPT_PARENT_ID] int)
insert [tb]
select 1,'部门名称1',0 union all
select 2,'部门名称2',1 union all
select 3,'部门名称3',1 union all
select 4,'部门名称4',3
--------------开始查询--------------------------
DECLARE @Dept_name nvarchar(20)
SET @Dept_name = N'部门名称3'
;WITH
DEPTS AS(
-- 定位点成员
SELECT * FROM tb
WHERE DEPT_NAME = @Dept_name
UNION ALL
-- 递归成员, 通过引用CTE自身与Dept基表JOIN实现递归
SELECT A.*
FROM tb A, DEPTS B
WHERE A.DEPT_PARENT_ID = B.DEPT_ID
)
SELECT * FROM DEPTS
GO----------------结果----------------------------
/*DEPT_ID DEPT_NAME DEPT_PARENT_ID
----------- --------- --------------
3 部门名称3 1
4 部门名称4 3(2 行受影响)
*/