菟丝子的问题。
菟丝子 16:55:40
我讲讲需求吧
菟丝子 16:56:00
SQL问题对于你来说简直就不是问题
菟丝子 16:59:17
是这样的,有一个组织单位表,结构是这样 parentCode,sCode sName三个字段,
菟丝子 16:59:52
现在输入一个sCode,需要找到这个单位的下级单位,下下级......
菟丝子 17:00:17
这个需要存储过程实现 ,存储过程有个输入参数就是这个@sCode
/*
Function:GetCorpTree
Released Time:20080613
Support by Roboth
*/
--create database for testing
CREATE DATABASE DB_Tree
GO
USE DB_Tree
GO
--create table including tree
CREATE TABLE S_Tree(Bh bigint ,Label varchar(50),ParentBh bigint)
GO
--insert data
INSERT S_Tree SELECT 1, 'People', 0
UNION ALL SELECT 2, 'Man' ,1
UNION ALL SELECT 3, 'Woman' ,1
UNION ALL SELECT 4, 'SWoman', 3
UNION ALL SELECT 5, 'BWoman', 3
GO
--get all list
SELECT * FROM S_Tree
--display such as
--1 People 0
--2 Man 1
--3 Woman 1
--4 SWoman 3
--5 BWoman 3

go
--create proc
CREATE PROC SP_GetCorpTree
@c bigint
AS
BEGIN
--obtain S_Tree's structure.
SELECT * INTO TmpTbl from S_Tree WHERE 1=2
--add column as Depth int
ALTER TABLE TmpTbl Add Depth int
DECLARE @Node int
DECLARE @Depth int
SET @Depth=1
SET @Node=@c
INSERT TmpTbl SELECT *,@Depth from S_Tree WHERE ParentBh=@Node
WHILE @@ROWCOUNT > 0
BEGIN
set @Depth = @Depth + 1
INSERT into TmpTbl
select *, @depth
from S_Tree Where ParentBh in (select Bh from TmpTbl WHERE depth=@depth-1)
END
SELECT * FROM TmpTbl
DROP TABLE TmpTbl
END
GO
EXECUTE SP_GetCorpTree @c =3

我讲讲需求吧
菟丝子 16:56:00
SQL问题对于你来说简直就不是问题
菟丝子 16:59:17
是这样的,有一个组织单位表,结构是这样 parentCode,sCode sName三个字段,
菟丝子 16:59:52
现在输入一个sCode,需要找到这个单位的下级单位,下下级......
菟丝子 17:00:17
这个需要存储过程实现 ,存储过程有个输入参数就是这个@sCode
/*
Function:GetCorpTree
Released Time:20080613
Support by Roboth
*/
--create database for testing
CREATE DATABASE DB_Tree
GO
USE DB_Tree
GO
--create table including tree
CREATE TABLE S_Tree(Bh bigint ,Label varchar(50),ParentBh bigint)
GO
--insert data
INSERT S_Tree SELECT 1, 'People', 0
UNION ALL SELECT 2, 'Man' ,1
UNION ALL SELECT 3, 'Woman' ,1
UNION ALL SELECT 4, 'SWoman', 3
UNION ALL SELECT 5, 'BWoman', 3
GO
--get all list
SELECT * FROM S_Tree
--display such as
--1 People 0
--2 Man 1
--3 Woman 1
--4 SWoman 3
--5 BWoman 3
go
--create proc
CREATE PROC SP_GetCorpTree
@c bigint
AS
BEGIN
--obtain S_Tree's structure.
SELECT * INTO TmpTbl from S_Tree WHERE 1=2
--add column as Depth int
ALTER TABLE TmpTbl Add Depth int
DECLARE @Node int
DECLARE @Depth int
SET @Depth=1
SET @Node=@c
INSERT TmpTbl SELECT *,@Depth from S_Tree WHERE ParentBh=@Node
WHILE @@ROWCOUNT > 0
BEGIN
set @Depth = @Depth + 1
INSERT into TmpTbl
select *, @depth
from S_Tree Where ParentBh in (select Bh from TmpTbl WHERE depth=@depth-1)
END
SELECT * FROM TmpTbl
DROP TABLE TmpTbl
END
GO
EXECUTE SP_GetCorpTree @c =3



浙公网安备 33010602011771号