怎样实现函数的循环调用

elvis_gao 2007-10-09 01:03:02
我有一张表a
id name des parentid path
1 张三 员工 null 张三
2 李四 员工 1 张三 >李四
3 小花 职工 2 张三 >李四 > >小花
4 小明 职工 1 张三 > >小明

表结构显示为上面内容
我现在想做的是。当我知道某个parentid的时候找出这个记录的全路径。path字段表示。
例如:parentid等于2的时候它的path的表示"张三 >李四 > >小花".
我说明一下:当parentid =2时小花的的上级是李四(因为李四的id是2),然而李四又有parentid值为1,那么小花所在的全路径存在两个上级。
小花的path的值就是:张三 >李四 > >小花

我的实现过程是用函数的循环调用(也可以看成是递归)

Create Table a
(id int identity(1,1), [name] varchar(50), des varchar(50), parentid int, [path] varchar(30))

insert a
select 'Zhang San', 'Employee', null, null
union all select 'Li Si', 'Employee', 1, null
union all select 'Xiao Hua', 'Staff', 2, null
union all select 'Xiao Meng', 'Staff', 1, null


create function GetPath(@id int)
returns varchar(30)--path字段的大小
as
begin
declare @parentid int
declare @path varchar(30)
select @parentid=[parentid] from a where object_name(id)= @id
select @path=[path] from a where object_name(id)= @id
if @parentid is not null
set @path = @path + '>' + dbo.GetPath(@parentid)
return @path
end
GO

update a set [path]=(select dbo.GetPath(id))

但是不好使,无法实现效果,高手看看我的程序哪里有错误,还是数据库循环调用函数不支持?如果这样可以实现,怎么做?
...全文
286 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
elvis_gao 2007-11-29
  • 打赏
  • 举报
回复


楼上 welove1983兄弟那个问题我也回帖了,哈哈,挺巧
CREATE table a(id int, parentID int, sClassName nvarchar(20))
insert into a
select 1, 0, '1111'
union all select 2, 1, '1111_1'
union all select 3, 2, '1111-1-1'
union all select 4, 3, '1111-1-1-1'
union all select 5, 1, '1111-2'


create FUNCTION getParentID(@id INT)
RETURNS NVARCHAR(50)
AS
BEGIN
declare @parentid int
DECLARE @allParentID NVARCHAR(50)
set @allParentID=''
select @parentid=[parentid] from a where id= @id
IF(@parentid <>0)
begin
set @allParentID = cast(@parentid as nvarchar) +','+ DBO.getParentID(@parentid)
end
return cast(@allParentID as nvarchar)
END

SELECT 'ParentID'=dbo.getParentID(4)

/*
ParentID
--------------------------------------------------
3,2,1,
*/

elvis_gao 2007-11-29
  • 打赏
  • 举报
回复

楼上兄弟那个问题我也回帖了,哈哈,挺巧
CREATE table a(id int, parentID int, sClassName nvarchar(20))
insert into a
select 1, 0, '1111'
union all select 2, 1, '1111_1'
union all select 3, 2, '1111-1-1'
union all select 4, 3, '1111-1-1-1'
union all select 5, 1, '1111-2'


create FUNCTION getParentID(@id INT)
RETURNS NVARCHAR(50)
AS
BEGIN
declare @parentid int
DECLARE @allParentID NVARCHAR(50)
set @allParentID=''
select @parentid=[parentid] from a where id= @id
IF(@parentid <>0)
begin
set @allParentID = cast(@parentid as nvarchar) +','+ DBO.getParentID(@parentid)
end
return cast(@allParentID as nvarchar)
END

SELECT 'ParentID'=dbo.getParentID(4)

/*
ParentID
--------------------------------------------------
3,2,1,
*/

welove1983 2007-10-09
  • 打赏
  • 举报
回复
结果...
1 Zhang San Employee NULL Zhang San
2 Li Si Employee 1 Zhang San>Li Si
3 Xiao Hua Staff 2 Zhang San>Li Si>Xiao Hua
4 Xiao Meng Staff 1 Zhang San>Xiao Meng
welove1983 2007-10-09
  • 打赏
  • 举报
回复
alter function GetPath(@id int)
returns varchar(30)
as
begin
declare @parentid int
declare @path varchar(30)
select @parentid=[parentid] from a where id= @id
select @path=[name] from a where id=@id
if @parentid is not null
set @path = dbo.GetPath(@parentid)+ '>' + @path
return @path
end
晓风残月0110 2007-10-09
  • 打赏
  • 举报
回复
一看很熟
up一下

34,590

社区成员

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

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