帮忙写个递归SQL函数,

ljz888666555 2010-04-26 06:08:55
测试数据。

deptID pdeptID DeptName sortID
1 0 湖南石油分公司 1
2 1 安全数质量管理处 8
3 1 财务资产处 5
4 1 发展规划处 7
6 1 公司领导 1
10 1 经理办公室 2
16 1 人力资源处 4
306 0 湖南销售分公司 2
307 306 安全数质量科 7
308 306 办公室 3
309 306 财务核算部 4
312 306 公司领导 2
316 306 物流部 11
317 306 信息工作站 10

要求:输入一个ID,返回路径部门名如
输入6,返回,湖南石油分公司\公司领导\

自己写的一个好像没有递归,

create function dbo.GetDeptName(@DeptID int,@t varchar(255))
returns varchar(255)
as
begin
declare @PID int
declare @DName varchar(255)
declare @temp varchar(255)

select @PID=PDeptID,@DName=DeptName from RTX_Dept where DeptID=@DeptID
set @t=@t+@DName+'\'
if(@PID<>0)
begin
set @temp=dbo.GetDeptName(@PID,@t)

return @t
end
...全文
143 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljz888666555 2010-04-26
  • 打赏
  • 举报
回复
结账给分。
abcdef1111111 2010-04-26
  • 打赏
  • 举报
回复
学习。。
feixianxxx 2010-04-26
  • 打赏
  • 举报
回复
declare @deptID int
set @deptID=6
;with cte as
(
select pdeptID ,path=cast(DeptName+'\' as varchar(100)) from RTX_Dept where deptID=@deptID
union all
select p.pdeptID,cast(p.DeptName+'\'+c.path as varchar(100)) from RTX_Dept p join cte c on p.deptID=c.pdeptID
)
select top 1 path
from cte
order by pdeptID
/*
path
----------------------------------------------------------------------------------------------------
湖南石油分公司\公司领导\*/
htl258_Tony 2010-04-26
  • 打赏
  • 举报
回复
--------------------------------------------------------------------------
-- Author : htl258(Tony)
-- Date : 2010-04-26 18:23:54
-- Version:Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
-- Blog : http://blog.csdn.net/htl258
--------------------------------------------------------------------------
--> 生成测试数据表:RTX_DEPT

IF NOT OBJECT_ID('[RTX_DEPT]') IS NULL
DROP TABLE [RTX_DEPT]
GO
CREATE TABLE [RTX_DEPT]([deptID] INT,[pdeptID] INT,[DeptName] NVARCHAR(10),[sortID] INT)
INSERT [RTX_DEPT]
SELECT 1,0,N'湖南石油分公司',1 UNION ALL
SELECT 2,1,N'安全数质量管理处',8 UNION ALL
SELECT 3,1,N'财务资产处',5 UNION ALL
SELECT 4,1,N'发展规划处',7 UNION ALL
SELECT 6,1,N'公司领导',1 UNION ALL
SELECT 10,1,N'经理办公室',2 UNION ALL
SELECT 16,1,N'人力资源处',4 UNION ALL
SELECT 306,0,N'湖南销售分公司',2 UNION ALL
SELECT 307,306,N'安全数质量科',7 UNION ALL
SELECT 308,306,N'办公室',3 UNION ALL
SELECT 309,306,N'财务核算部',4 UNION ALL
SELECT 312,306,N'公司领导',2 UNION ALL
SELECT 316,306,N'物流部',11 UNION ALL
SELECT 317,306,N'信息工作站',10
GO
--SELECT * FROM [RTX_DEPT]

-->SQL查询如下:
IF OBJECT_ID('dbo.GetDeptName') IS NOT NULL
DROP FUNCTION dbo.GetDeptName
GO
CREATE FUNCTION dbo.GetDeptName(@DeptID int)
RETURNS NVARCHAR(100)
AS
BEGIN
DECLARE @P INT,@S VARCHAR(1000)
WHILE EXISTS(SELECT 1 FROM [RTX_DEPT] WHERE DEPTID = @DEPTID)
BEGIN
SELECT @S = [DEPTNAME]+'\'+ISNULL(@S,''),@P = [PDEPTID]
FROM [RTX_DEPT]
WHERE DEPTID = @DEPTID
SET @DEPTID = @P
END
RETURN @S
END
GO

SELECT dbo.GetDeptName(6) AS path
/*
path
-----------------------------------------
湖南石油分公司\公司领导\

(1 行受影响)
*/
htl258_Tony 2010-04-26
  • 打赏
  • 举报
回复
--------------------------------------------------------------------------
-- Author : htl258(Tony)
-- Date : 2010-04-26 18:23:54
-- Version:Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
-- Blog : http://blog.csdn.net/htl258
--------------------------------------------------------------------------
--> 生成测试数据表:RTX_DEPT

IF NOT OBJECT_ID('[RTX_DEPT]') IS NULL
DROP TABLE [RTX_DEPT]
GO
CREATE TABLE [RTX_DEPT]([deptID] INT,[pdeptID] INT,[DeptName] NVARCHAR(10),[sortID] INT)
INSERT [RTX_DEPT]
SELECT 1,0,N'湖南石油分公司',1 UNION ALL
SELECT 2,1,N'安全数质量管理处',8 UNION ALL
SELECT 3,1,N'财务资产处',5 UNION ALL
SELECT 4,1,N'发展规划处',7 UNION ALL
SELECT 6,1,N'公司领导',1 UNION ALL
SELECT 10,1,N'经理办公室',2 UNION ALL
SELECT 16,1,N'人力资源处',4 UNION ALL
SELECT 306,0,N'湖南销售分公司',2 UNION ALL
SELECT 307,306,N'安全数质量科',7 UNION ALL
SELECT 308,306,N'办公室',3 UNION ALL
SELECT 309,306,N'财务核算部',4 UNION ALL
SELECT 312,306,N'公司领导',2 UNION ALL
SELECT 316,306,N'物流部',11 UNION ALL
SELECT 317,306,N'信息工作站',10
GO
--SELECT * FROM [RTX_DEPT]

-->SQL查询如下:
IF OBJECT_ID('dbo.GetDeptName') IS NOT NULL
DROP FUNCTION dbo.GetDeptName
GO
CREATE FUNCTION dbo.GetDeptName(@DeptID int)
RETURNS NVARCHAR(100)
AS
begin
declare @p int,@s varchar(1000)
while exists(select 1 from [RTX_DEPT] where DeptID = @DeptID)
begin
select @s = [DeptName]+isnull('\'+@s,''),@p = [pdeptID] from [RTX_DEPT] where DeptID = @DeptID
set @DeptID = @p
end
return @s
end
GO

SELECT dbo.GetDeptName(6) AS path
/*
path
----------------------------------------------------------------------------------------------------
湖南石油分公司\公司领导

(1 行受影响)
*/
东那个升 2010-04-26
  • 打赏
  • 举报
回复
create table RTX_Dept(deptID int,pdeptID int,DeptName varchar(20),sortID int)
insert RTX_Dept select 1, 0 ,'湖南石油分公司', 1
insert RTX_Dept select 2, 1 ,'安全数质量管理处', 8
insert RTX_Dept select 3, 1 ,'财务资产处', 5
insert RTX_Dept select 4, 1 ,'发展规划处', 7
insert RTX_Dept select 6, 1 ,'公司领导', 1
insert RTX_Dept select 10, 1 ,'经理办公室', 2
insert RTX_Dept select 16, 1 ,'人力资源处', 4
insert RTX_Dept select 306, 0 ,'湖南销售分公司', 2
insert RTX_Dept select 307, 306 ,'安全数质量科', 7
insert RTX_Dept select 308, 306 ,'办公室', 3
insert RTX_Dept select 309, 306 ,'财务核算部', 4
insert RTX_Dept select 312, 306 ,'公司领导', 2
insert RTX_Dept select 316, 306 ,'物流部', 11
insert RTX_Dept select 317, 306 ,'信息工作站', 10



create function get_path(@id int)
returns varchar(200)
as begin

declare @path varchar(200)
declare @pdeptID int

select @path=DeptName+'\' ,@pdeptID=pdeptID from RTX_Dept where deptID=@id


while @@rowcount>0
begin
select @path=DeptName+'\'+@path,@pdeptID=pdeptID from RTX_Dept where deptID=@pdeptID
end
return @path
end

select dbo.get_path(6)

-----------------------------------
湖南石油分公司\公司领导\
东那个升 2010-04-26
  • 打赏
  • 举报
回复
create table RTX_Dept(deptID int,pdeptID int,DeptName varchar(20),sortID int)
insert RTX_Dept select 1, 0 ,'湖南石油分公司', 1
insert RTX_Dept select 2, 1 ,'安全数质量管理处', 8
insert RTX_Dept select 3, 1 ,'财务资产处', 5
insert RTX_Dept select 4, 1 ,'发展规划处', 7
insert RTX_Dept select 6, 1 ,'公司领导', 1
insert RTX_Dept select 10, 1 ,'经理办公室', 2
insert RTX_Dept select 16, 1 ,'人力资源处', 4
insert RTX_Dept select 306, 0 ,'湖南销售分公司', 2
insert RTX_Dept select 307, 306 ,'安全数质量科', 7
insert RTX_Dept select 308, 306 ,'办公室', 3
insert RTX_Dept select 309, 306 ,'财务核算部', 4
insert RTX_Dept select 312, 306 ,'公司领导', 2
insert RTX_Dept select 316, 306 ,'物流部', 11
insert RTX_Dept select 317, 306 ,'信息工作站', 10



create function get_path(@id int)
returns varchar(200)
as begin

declare @path varchar(200)
declare @pdeptID int

select @path=DeptName+'\' ,@pdeptID=pdeptID from RTX_Dept where deptID=@id


while @@rowcount>0
begin
select @path=DeptName+'\'+@path,@pdeptID=pdeptID from RTX_Dept where deptID=@pdeptID
end
return @path
end

select dbo.get_path(6)

-----------------------------------
湖南石油分公司\公司领导\

(1 行受影响)







22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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