请教大家个难题

kinthtime 2005-11-24 11:18:21
Deptid deptname parentid nodetype
84 天府集团 NULL 0
85 天府(深圳)分公司 84 0
86 天府(广州)分公司 84 0
87 销售部 86 1
88 市场部 86 1
89 产品代理商 86 2
90 经销商 85 2
91 分销商 89 2
92 Dept1 84 0
93 Dept2 92 0
94 Dept3 93 0
95 Dept4 94 0
96 Dept5 95 0
97 Dept6 96 0
98 Dept7 97 0
99 产品代理 84 2
100 dept8 98 0
101 华南区 84 2
102 华南-1 101 2
103 华-1 101 2

nodetype为0时返回本身,比如:@intdeptid=86 时,则返回:天府(广州)分公司
nodetype为1时返回上级(可能是上1级,2级或n级,直到该级的nodetype值为0)如@intdeptid=88 则返回:天府(广州)分公司
nodetype为2时返回上一级 如:@intdeptid=103 则返回华南区

该怎么写这个存储过程呢
...全文
104 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
samfeng_2003 2005-11-24
  • 打赏
  • 举报
回复
@intdeptid=92的时候
----------- -------------------- ----------- -----------
92 Dept1 84 0

(所影响的行数为 1 行)


@intdeptid=103的时候
----------- -------------------- ----------- -----------
101 华南区 84 2

(所影响的行数为 1 行)


@intdeptid=85的时候
----------- -------------------- ----------- -----------
85 天府(深圳)分公司 84 0

(所影响的行数为 1 行)

@intdeptid=88的时候
----------- -------------------- ----------- -----------
86 天府(广州)分公司 84 0

(所影响的行数为 1 行)

测试了一下,应该符合要求吧!




samfeng_2003 2005-11-24
  • 打赏
  • 举报
回复
/*--------------去掉create前的括号----------*/
你可以根据你的要求改成存储过程!
[create] table t
(Deptid int,deptname varchar(20),parentid int,nodetype int)

insert t values (84,'天府集团',null,0)
insert t values (85,'天府(深圳)分公司',84,0)
insert t values (86,'天府(广州)分公司',84,0)
insert t values (87,'销售部',86,1)
insert t values (88,'市场部',86,1)
insert t values (89,'产品代理商',86,2)
insert t values (90,'经销商',85,2)
insert t values (91,'分销商',89,2)
insert t values (92,'Dept1',84,0)
insert t values (93,'Dept2',92,0)
insert t values (94,'Dept3',93,0)
insert t values (95,'Dept4',94,0)
insert t values (96,'Dept5',95,0)
insert t values (97,'Dept6',96,0)
insert t values (98,'Dept7',97,0)
insert t values (99,'产品代理',84,2)
insert t values (100,'Dept8',98,0)
insert t values (101,'华南区',84,2)
insert t values (102,'华南-1',101,2)
insert t values (103,'华-1',101,2)

declare @intdeptid int
set @intdeptid=92


select case when b.nodetype>0 then a.Deptid else b.deptid end
,case when b.nodetype>0 then a.deptname else b.deptname end,
case when b.nodetype>0 then a.parentid else b.parentid end,
case when b.nodetype>0 then a.nodetype else b.nodetype end
from t a,t b where a.deptid=b.parentid and b.deptid=@intdeptid

drop table t
lsqkeke 2005-11-24
  • 打赏
  • 举报
回复
--传入Deptid编号
create proc search
@intdeptid int
as
declare @pid int
declare @ntype int
declare @name1 varchar(100)

select @pid=nodetype,@ntype=parentid,@name1=deptname from table1 where deptid=@intdeptid
if @pid=0
begin
select dname=@name1
return
end
else
begin
set @intdeptid=@ntype
exec search @intdeptid
end
go
soulwin 2005-11-24
  • 打赏
  • 举报
回复
用递归是好,可是递归的层数不能超过32
mislrb 2005-11-24
  • 打赏
  • 举报
回复
每次我碰到BOM就头大,以前也看过不上递归的过程,看的云里雾里,今天看了 lsqkeke的方法真是简单易懂.
mislrb 2005-11-24
  • 打赏
  • 举报
回复
请没考虑递归的楼上朋友们添加以下几条记录试试你们的过程或函数

insert t values (104,'华南区aaa',101,3)
insert t values (105,'华南区bbb',104,4)
insert t values (106,'华南区ccc',105,5)



lsqkeke的方法简单正确,佩服

mislrb 2005-11-24
  • 打赏
  • 举报
回复
请没考虑递归的楼上朋友们添加以下几条记录试试你们的过程或函数

insert t values (104,'华南区aaa',101,3)
insert t values (105,'华南区bbb',104,4)
insert t values (106,'华南区ccc',105,5)

一楼的兄弟不错,牛
zhuangdefeng 2005-11-24
  • 打赏
  • 举报
回复
函数,测试通过
zhuangdefeng 2005-11-24
  • 打赏
  • 举报
回复
drop function fm_f_getfatherid
go
create function fm_f_getfatherid(@id bigint)
returns bigint
--7.得到指定id的父id
--select * from f_getparentid(11)
as
begin
declare @pid bigint
declare @nodetype bigint
declare @nr varchar(800)
select @pid=parentid ,@nodetype=nodetype from t where deptid=@id
if (@nodetype=0)
begin

return @id
end
if (@nodetype=1 )
begin
while @pid<>0
begin
select @id=@pid

select @pid=parentid from t where deptid=@id
end
return(@id)
end
if (@nodetype=2)
begin
select @pid=parentid from t where deptid=@id
end
return @pid
end

select deptname from t where deptid in (select 数据库名.dbo.fm_f_getfatherid(86) from t )

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧