一个部门的数据表,里面是目录树结构.
如: deptID deptName parentID
里面的数据 是 1 所有部门 0
2 财务部 1
3 市场部 1
4 仓库管理 1
5 东北市场 3
6 西北市场 3
7 东南市场 3
8 上海市 7
9 北京市 6等依次类推得目录树结构
我想写一个函数 传入部门ID号后 马上得到相应的 部门结构 如
输入8 得到的是 市场部-东南市场-上海市
输入9 得到的是 市场部-西北市场-北京市
输入6 得到的是 市场部-西北市场输入3 得到的是 市场部输入1 得到的是 所有部门
如: deptID deptName parentID
里面的数据 是 1 所有部门 0
2 财务部 1
3 市场部 1
4 仓库管理 1
5 东北市场 3
6 西北市场 3
7 东南市场 3
8 上海市 7
9 北京市 6等依次类推得目录树结构
我想写一个函数 传入部门ID号后 马上得到相应的 部门结构 如
输入8 得到的是 市场部-东南市场-上海市
输入9 得到的是 市场部-西北市场-北京市
输入6 得到的是 市场部-西北市场输入3 得到的是 市场部输入1 得到的是 所有部门
解决方案 »
- 求一字符串截取sql语句,在线等。
- SQL2000的数据附加到2008后,找不到存储过程
- 请教rs.last()的正确使用
- 还原数据库后提示正在装载----~~~~~~~总是正在装载.这可怎么办?
- 一个很简单的两个表的连接查询。用在asp上,要快速,希望得到解答。
- 按flag分组,再相加value,但是重复的number只能加一次
- 这种情况如何写查询语句?
- 请教一个触发器问题>
- 中心数据库和各地方数据库之间的数据,这样的情况如何处理比较好
- SQL语句分析 定高分相送
- 郁闷啦,谁能救我写出来这句,给800分,不给我是大尾巴蛆.急~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 用int类型就出错,请指教
insert into dept select 1, '所有部门', 0
union all select 2, '财务部' , 1
union all select 3 , '市场部' , 1
union all select 4 , '仓库管理' , 1
union all select 5, '东北市场' , 3
union all select 6 , '西北市场' , 3
union all select 7 , '东南市场' , 3
union all select 8 , '上海市' , 7
union all select 9 , '北京市' , 6
CREATE function GetdeptName(@deptID varchar(10))
returns varchar(4000)
as
begin
declare @re table(deptID int,level int,ename varchar(4000))
declare @level int,@name varchar(1000)
set @name=''
set @level=0
insert @re select @deptID,@level,@name
while @@rowcount>0
begin
set @level=@level+1
insert @re select a.ParentID,@level,(deptName+'→'+@name) from dept as a,@re as b
where a.deptID=b.deptID and b.level=@level-1 and a.ParentID!=0
select @name=ename from @re where level=@level
end
select @name=ename from @re where level in (select max(level) from @re)
select @name=left(@name,len(@name)-1)
return @name
end
insert test
select 1 ,'所有部门', 0 union all
select 2 ,'财务部', 1 union all
select 3 ,'市场部', 1 union all
select 4 ,'仓库管理', 1 union all
select 5 ,'东北市场', 3 union all
select 6 ,'西北市场', 3 union all
select 7 ,'东南市场', 3 union all
select 8 ,'上海市', 7 union all
select 9 ,'北京市', 6
--select * from test
gocreate proc pTest
@id int
as
begin
declare @result varchar(1000)
declare @ProvID int
select @ProvID=@id select @result=deptName from test where deptID=@ProvID
while exists(select 1 from test where deptID=@ProvID and parentID<>0)
begin
select @ProvID=parentID from test where deptID=@ProvID
select @result=deptName +'-'+@result from test where deptID=@ProvID
end select @result
end
goexec pTest 3drop proc pTest
drop table test
我是建了一个函数
调用时输入要求的deptid就成
例如:
select dbo.GetdeptName(8)
--结果:
市场部→东南市场→上海市
GO
CREATE TABLE tbDept
(
deptID INT,
deptName VARCHAR(50),
parentID INT
)INSERT INTO tbDept
SElECT 1,'所有部门',0 UNION ALL
SElECT 2, '财务部', 1 UNION ALL
SElECT 3, '市场部', 1 UNION ALL
SElECT 4, '仓库管理',1 UNION ALL
SElECT 5, '东北市场',3 UNION ALL
SElECT 6, '西北市场',3 UNION ALL
SElECT 7, '东南市场',3 UNION ALL
SElECT 8, '上海市', 7 UNION ALL
SElECT 9, '北京市', 6SELECT *
FROM tbDeptGO
CREATE FUNCTION GetDeptStruct
(@deptID INT)
RETURNS VARCHAR(500)
AS
BEGINDECLARE @s VARCHAR(500), @dID INTSELECT @s = deptName, @dID = @deptID
FROM tbDept
WHERE deptID = @deptIDWHILE @@ROWCOUNT > 1
BEGIN
SELECT @s = b.deptName + '-' + @s, @dID = b.deptID
FROM tbDept a
INNER JOIN tbDept b ON a.parentID = b.deptID
WHERE a.deptID = @dID
AND b.deptID > 1
ENDRETURN @sEND
GOSELECT dbo.GetDeptStruct(2)GO
ROLLBACK
“WHILE @@ROWCOUNT > 1”
应为
“WHILE @@ROWCOUNT > 0”
那就只需对下面这句添加一个对传入的的配套ID作判断来分条件
if(@deptID!=1)
insert @re select a.ParentID,@level,(deptName+'→'+@name) from dept
as a,@re as b where a.deptID=b.deptID and b.level=@level-1 and a.ParentID!=0
else
insert @re select a.ParentID,@level,(deptName+'→'+@name) from dept
as a,@re as b where a.deptID=b.deptID and b.level=@level-1
create table TableA(deptID int,deptName nvarchar(100),parentID int)
insert into TableA
select 1,'所有部门',0 union all
select 2,'财务部', 1 union all
select 3,'市场部', 1 union all
select 4,'仓库管理',1 union all
select 5,'东北市场',3 union all
select 6,'西北市场',3 union all
select 7,'东南市场',3 union all
select 8,'上海市', 7 union all
select 9,'北京市', 6--建立函数
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[AllDept]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[AllDept]
GOCREATE function AllDept(@iDeptID int)
returns nvarchar(1000)
as
begin
declare @vReturnValue nvarchar(1000)
,@iParentID int
,@vCurrentDeptName nvarchar(200)
select @vReturnValue=''
,@vCurrentDeptName=''
if(exists(select top 1 0 from TableA where DeptID=@iDeptID and parentID=0))
begin
select @vReturnValue =@vReturnValue+deptName+'-'
from TableA
where parentID<>0
return (@vReturnValue)
end
if(exists(select top 1 0 from TableA where DeptID=@iDeptID and parentID=1))
begin
select @vReturnValue=@vReturnValue+deptName
from TableA where DeptID=@iDeptID and parentID=1
--return (@vReturnValue)
--set @vReturnValue=@vReturnValue+dbo.AllDept(@iParentID)
end
else
begin
select @iParentID=parentID
,@vCurrentDeptName=deptName
from TableA
where DeptID=@iDeptID
set @vReturnValue=@vReturnValue+@vCurrentDeptName+'-'+dbo.AllDept(@iParentID)
--return (@vReturnValue)
end
return (@vReturnValue)
endGO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GOselect *,dbo.AllDept(deptid) AllDeptName from tableA
--显示结果
deptId deptName AllDeptName
1 所有部门 0 财务部-市场部-仓库管理-东北市场-西北市场-东南市场-上海市-北京市-
2 财务部 1 财务部
3 市场部 1 市场部
4 仓库管理 1 仓库管理
5 东北市场 3 东北市场-市场部
6 西北市场 3 西北市场-市场部
7 东南市场 3 东南市场-市场部
8 上海市 7 上海市-东南市场-市场部
9 北京市 6 北京市-西北市场-市场部
--删除测试环境
drop table tableA
drop table dbo.AllDept