求一个SQL递归函数写法

jiangbo1125 2011-01-25 09:35:56
表的字段是这样的
GroupID ParentGroupID Deepth
1 0 1
2 1 2
3 1 2
4 2 3

想写个函数,f(int) 传入一个GroupID,返回deepth=1的GroupID,例如上面我传4,希望可以返回1.
...全文
488 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
黄_瓜 2011-01-25
  • 打赏
  • 举报
回复
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([GroupID] int,[ParentGroupID] int,[Deepth] int)
insert [tb]
select 1,0,1 union all
select 2,1,2 union all
select 3,1,2 union all
select 4,2,3

if object_id('ff','fn') is not null drop function ff
go
create function ff(@GroupID int)
returns int
as
begin
declare @i int
;with t as
(
select *,lev=1/*递归次数,你需要的应该是递归最后一层的数据*/ from [tb] where [GroupID]=4
union all
select a.*,lev=lev+1 from [tb] a join t on a.[GroupID]=t.[ParentGroupID]
)
select top 1 @i=[GroupID] from t order by lev desc
return @i
end
--调用
select dbo.ff(4)
Shawn 2011-01-25
  • 打赏
  • 举报
回复
CREATE TABLE #temp
(
GroupID INT,
ParentGroupID INT,
Deepth INT
)
INSERT INTO #temp
SELECT 1, 0,1 UNION ALL
SELECT 2, 1,2 UNION ALL
SELECT 3, 1,2 UNION ALL
SELECT 4, 2,3

--SELECT * FROM #temp

;WITH cte_test AS
(
SELECT ParentGroupID FROM #temp WHERE GroupID = 4
UNION ALL
SELECT a.ParentGroupID FROM #temp a, cte_test b WHERE a.GroupID = b.ParentGroupID
)
SELECT TOP(1) * FROM cte_test WHERE ParentGroupID > 0 ORDER BY ParentGroupID
飘零一叶 2011-01-25
  • 打赏
  • 举报
回复
create table tb
(
GroupID int,
ParentGroupID int,
Deepth int
)
insert into tb
select 1,0,1 union all
select 2,1,2 union all
select 3,1,2 union all
select 4,2,3

create function find_deepth1(@GroupID int)
returns int
as
begin
declare @ID int
;with cte as
(
select * from tb where GroupID=GroupID
union all
select a.* from tb a join cte b on a.ParentGroupID=b.GroupID
)
select @ID=GroupID from cte where Deepth=1
return @ID
end

select dbo.find_deepth1(4)
AcHerat 元老 2011-01-25
  • 打赏
  • 举报
回复

--有点不大懂LZ的意思!deepth = 1的时候应该不用传入groupID了吧!
select GroupId from tb where deepth = 1
王向飞 2011-01-25
  • 打赏
  • 举报
回复
/*
标题:SQL SERVER 2000中查询指定节点及其所有父节点的函数(表格形式显示)
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*/

create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go

--查询指定节点及其所有父节点的函数
create function f_pid(@id varchar(3)) returns @t_level table(id varchar(3))
as
begin
insert into @t_level select @id
select @id = pid from tb where id = @id and pid is not null
while @@ROWCOUNT > 0
begin
insert into @t_level select @id
select @id = pid from tb where id = @id and pid is not null
end
return
end
go

--调用函数查询002(广州市)及其所有父节点
select a.* from tb a , f_pid('002') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
002 001 广州市

(所影响的行数为 2 行)
*/

--调用函数查询003(深圳市)及其所有父节点
select a.* from tb a , f_pid('003') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
003 001 深圳市

(所影响的行数为 2 行)
*/

--调用函数查询008(西乡镇)及其所有父节点
select a.* from tb a , f_pid('008') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
003 001 深圳市
007 003 宝安区
008 007 西乡镇

(所影响的行数为 4 行)
*/

drop table tb
drop function f_pid
王向飞 2011-01-25
  • 打赏
  • 举报
回复
/*
标题:SQL SERVER 2000中查询指定节点及其所有子节点的函数(表格形式显示)
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*/

create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go

--查询指定节点及其所有子节点的函数
create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @id , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.id , @level
from tb a , @t_Level b
where a.pid = b.id and b.level = @level - 1
end
return
end
go

--调用函数查询001(广东省)及其所有子节点
select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
002 001 广州市
003 001 深圳市
004 002 天河区
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇

(所影响的行数为 10 行)
*/

--调用函数查询002(广州市)及其所有子节点
select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
002 001 广州市
004 002 天河区

(所影响的行数为 2 行)
*/

--调用函数查询003(深圳市)及其所有子节点
select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
003 001 深圳市
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇

(所影响的行数为 7 行)
*/

drop table tb
drop function f_cid



SQL code
@@ROWCOUNT:返回受上一语句影响的行数。
返回类型:integer。
注释:任何不返回行的语句将这一变量设置为 0 ,如 IF 语句。
示例:下面的示例执行 UPDATE 语句并用 @@ROWCOUNT 来检测是否有发生更改的行。

UPDATE authors SET au_lname = 'Jones' WHERE au_id = '999-888-7777'
IF @@ROWCOUNT = 0
print 'Warning: No rows were updated'

结果:

(所影响的行数为 0 行)
Warning: No rows were updated





/*
标题:SQL SERVER 2005中查询指定节点及其所有子节点的方法(表格形式显示)
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2010-02-02
地点:新疆乌鲁木齐
*/

create table tb(id varchar(3) , pid varchar(3) , name nvarchar(10))
insert into tb values('001' , null , N'广东省')
insert into tb values('002' , '001' , N'广州市')
insert into tb values('003' , '001' , N'深圳市')
insert into tb values('004' , '002' , N'天河区')
insert into tb values('005' , '003' , N'罗湖区')
insert into tb values('006' , '003' , N'福田区')
insert into tb values('007' , '003' , N'宝安区')
insert into tb values('008' , '007' , N'西乡镇')
insert into tb values('009' , '007' , N'龙华镇')
insert into tb values('010' , '007' , N'松岗镇')
go

DECLARE @ID VARCHAR(3)

--查询ID = '001'的所有子节点
SET @ID = '001'
;WITH T AS
(
SELECT ID , PID , NAME
FROM TB
WHERE ID = @ID
UNION ALL
SELECT A.ID , A.PID , A.NAME
FROM TB AS A JOIN T AS B ON A.PID = B.ID
)
SELECT * FROM T ORDER BY ID
/*
ID PID NAME
---- ---- ----------
001 NULL 广东省
002 001 广州市
003 001 深圳市
004 002 天河区
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇

(10 行受影响)
*/

--查询ID = '002'的所有子节点
SET @ID = '002'
;WITH T AS
(
SELECT ID , PID , NAME
FROM TB
WHERE ID = @ID
UNION ALL
SELECT A.ID , A.PID , A.NAME
FROM TB AS A JOIN T AS B ON A.PID = B.ID
)
SELECT * FROM T ORDER BY ID
/*
ID PID NAME
---- ---- ----------
002 001 广州市
004 002 天河区

(2 行受影响)
*/

--查询ID = '003'的所有子节点
SET @ID = '003'
;WITH T AS
(
SELECT ID , PID , NAME
FROM TB
WHERE ID = @ID
UNION ALL
SELECT A.ID , A.PID , A.NAME
FROM TB AS A JOIN T AS B ON A.PID = B.ID
)
SELECT * FROM T ORDER BY ID
/*
ID PID NAME
---- ---- ----------
003 001 深圳市
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇

(7 行受影响)
*/

drop table tb

--注:除ID值不一样外,三个SQL语句是一样的。



叶子 2011-01-25
  • 打赏
  • 举报
回复
你传入什么都返回deepth=1的GroupID?
那就不需要参数了?

直接嵌套子查询就可以了。
select GroupID from where deepth=1
内容概要:本报告基于寻汇与万事达卡在2026年联合发布的《超越自动化:定义智能体驱动的全球支付》白皮书,系统分析了AI智能体在B2B跨境支付领域的应用与发展。报告指出,传统跨境支付存在效率低、人工干预多、合规风险高等问题,当前正从数字化、数据化迈向“自主化”新阶段。AI智能体可在授权下自主完成支付、换汇、合规审核、对账等全流程操作,核心技术包括深度强化学习、自然语言处理和图神经网络,用于路径优化、合规解析与异常检测。报告揭示了决策可解释性不足、跨系统协同标准缺失、安全审计机制缺位三大研究空白,并探讨了法律责任归属、监管碎片化、数据主权与技术可靠性四大现实挑战。寻汇与万事达卡的合作构建了“智能体编排引擎”与全球合规决策网络,首次提出L0-L5的智能体自主化等级框架,推动行业标准化。预计2026至2027年将实现首批大规模商业部署,提升支付效率超30%。; 适合人群:金融科技研究人员、AI技术开发者、跨境支付行业从业者、企业财资管理人员及政策监管机构相关人员。; 使用场景及目标:①理解AI智能体在跨境支付中的技术架构与应用场景;②把握自主化支付的演进趋势与商业化前景;③为金融机构和技术公司布局AI驱动型支付系统提供战略参考;④助力监管机构制定适应智能体时代的合规框架。; 阅读建议:本报告兼具技术深度与产业视野,建议结合白皮书原文及相关技术文献对照研读,重点关注智能体决策逻辑、合规实现机制与跨系统集成方案,并关注后续试点项目的实际成效与监管反馈。

34,875

社区成员

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

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