帮帮忙,看看这个递归怎么写

hphong 2005-10-26 05:05:48

ID CoustomID
1 2
2 3
58 12
5 6
12 23
3 9

其实其中有关系 CoustomID = 9时 ID=3,继续查CoustomID = 3时ID = 2 ……依此类推
如何写条语句,当输入CoustomID = 9时,要求得到串'1,2,3,9'
是否递归?
...全文
253 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hphong 2005-11-16
  • 打赏
  • 举报
回复
create table A
(
ID int,
CoustomID int
)
insert A
select 1, 2 union
select 2, 3 union
select 58, 12 union
select 5, 6 union
select 12, 23 union
select 3, 9
go

--创建函数
create function f_str(@CoustomID int)
returns varchar(8000)
as
begin
declare @tb table(num int identity,CoustomID int)
insert @tb(CoustomID)
select distinct @CoustomID from A where exists(select 1 from A where CoustomID=@CoustomID)

while @@rowcount>0
begin
insert @tb(CoustomID)
select A.ID
from A
join @tb B on A.CoustomID=B.CoustomID
where not exists(select 1 from @tb where CoustomID=A.ID)
end

declare @str varchar(8000)
select @str=''
select @str=@str+','+convert(varchar,CoustomID) from @tb order by num desc
return stuff(@str,1,1,'')
end
go

--查询
select dbo.f_str(9)

--删除测试环境
drop function f_str
drop table A
zzit0721 2005-10-27
  • 打赏
  • 举报
回复

create table a (id int,zid int) -----建立测试数据
insert into a select 1,2
insert into a select 2,3
insert into a select 58,12
insert into a select 5,6
insert into a select 12,23
insert into a select 3,9

create function xy(@x int,@y int)---建立函数
returns int
as
begin
declare
@a int
while exists(select 1 from a where zid=@y)
begin
select @a=id from a where zid=@y
if @a=@x
begin
return 1
end
else
set @y=@a
end
return 0
end



select * -----查询
from (select *
from (select id
from a) a ,(select zid from a) b
where dbo.xy(a.id,b.zid)=1 ) cc
where id=1


drop table a ---销毁测试数据
drop function xy
vivianfdlpw 2005-10-27
  • 打赏
  • 举报
回复
create table A
(
ID int,
CoustomID int
)
insert A
select 1, 2 union
select 2, 3 union
select 58, 12 union
select 5, 6 union
select 12, 23 union
select 3, 9
go

--创建函数
create function f_str(@CoustomID int)
returns varchar(8000)
as
begin
declare @tb table(num int identity,CoustomID int)
insert @tb(CoustomID)
select distinct @CoustomID from A where exists(select 1 from A where CoustomID=@CoustomID)

while @@rowcount>0
begin
insert @tb(CoustomID)
select A.ID
from A
join @tb B on A.CoustomID=B.CoustomID
where not exists(select 1 from @tb where CoustomID=A.ID)
end

declare @str varchar(8000)
select @str=''
select @str=@str+','+convert(varchar,CoustomID) from @tb order by num desc
return stuff(@str,1,1,'')
end
go

--查询
select dbo.f_str(9)

--删除测试环境
drop function f_str
drop table A

--结果
/*
1,2,3,9

(所影响的行数为 1 行)
*/
samfeng_2003 2005-10-27
  • 打赏
  • 举报
回复
[create] table t
(id int,CoustomID int)

insert t values (1,2)
insert t values (2,3)
insert t values (58,12)
insert t values (5,6)
insert t values (12,23)
insert t values (3,9)

go
create function f_xun(@id int)
returns varchar(200)
as
begin
declare @t table(id int,CoustomID int,level int)
declare @i int,@re varchar(200)
set @i=1

set @i=1
set @re=''

insert into @t
select id,CoustomID,@i from t where Coustomid=@id

while @@rowcount<>0
begin
set @i=@i+1

insert into @t
select a.id,a.CoustomID,@i
from t a,@t b
where a.Coustomid=b.id and b.level=@i-1
end
select @re=@re+','+cast(id as varchar) from @t order by id asc
return(stuff(@re+','+cast(@id as varchar),1,1,''))
end
go

select id,Coustomid=dbo.f_xun(Coustomid) from t where Coustomid=9

drop function f_xun
drop table t

id Coustomid
-------------- ----------------------------------------------
3 1,2,3,9

(所影响的行数为 1 行)
hphong 2005-10-27
  • 打赏
  • 举报
回复
楼上的,这个表GetNextDept哪里去了?
Well 2005-10-26
  • 打赏
  • 举报
回复
/*
不知引用那位
*/
--建立測試環境
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tCore_Dept]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[tCore_Dept]
GO

CREATE TABLE [dbo].[tCore_Dept] (
[DeptID] [varchar] (8) COLLATE Chinese_Taiwan_Stroke_CI_AS NOT NULL ,
[DeptName] [varchar] (80) COLLATE Chinese_Taiwan_Stroke_CI_AS NOT NULL ,
[SuperiorDeptID] [varchar] (8) COLLATE Chinese_Taiwan_Stroke_CI_AS NOT NULL
) ON [PRIMARY]
GO

/*
--相應字段對應數據
A A Root
AA AA A
AB AB A
ABA ABA AB
ABAA ABAA ABA
B B Root
BA BA B
C C Root
*/
*/
CREATE Procedure GetNextDeptTree
(@Child_node varchar(8))
--WITH ENCRYPTION
As
begin
SET NOCOUNT ON
DECLARE @Cnt int
Declare @i int
Declare @tmpnode varchar(8)

create table #stack (node varchar(8))
create table #stackTmp (node varchar(8))
create table #stackTmpXXX (node varchar(8))

insert into #stack
select DeptID from GetNextDept(@Child_node)

insert into #stackTmp
select DeptID from GetNextDept(@Child_node)

select @tmpnode = @Child_node
select @Cnt = count(*) from tCore_Dept
select @i = 0

loops:

select * from #stack
declare cur cursor for
select node from #stackTmp
open cur
fetch next from cur into @tmpnode
while @@FETCH_STATUS = 0
begin
if(ltrim(@tmpnode)='')
print 'NULL'
else
print @tmpnode
insert into #stack select DeptID from GetNextDept(@tmpnode)
insert into #stackTmpXXX select DeptID from GetNextDept(@tmpnode)
select @i = @i + 1
fetch next from cur into @tmpnode
end
CLOSE cur
DEALLOCATE cur

select @i = @i + 1

delete from #stackTmp
insert into #stackTmp select node from #stackTmpXXX
delete from #stackTmpXXX

if (@i >= @Cnt * 1.5 )
goto ends

goto loops

ends:
select node from #stack
end
GO
vivianfdlpw 2005-10-26
  • 打赏
  • 举报
回复
递归限制32层以内,用循环比较好,参看:


--树形数据查询示例
--作者: 邹建

if exists (select * from dbo.sysobjects where id = object_id(N'[tb]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [tb]
GO

--示例数据
create table [tb]([id] int identity(1,1),[pid] int,name varchar(20))
insert [tb] select 0,'中国'
union all select 0,'美国'
union all select 0,'加拿大'
union all select 1,'北京'
union all select 1,'上海'
union all select 1,'江苏'
union all select 6,'苏州'
union all select 7,'常熟'
union all select 6,'南京'
union all select 6,'无锡'
union all select 2,'纽约'
union all select 2,'旧金山'
go

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_id]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_id]
GO

/*--树形数据处理

级别及排序字段

--邹建 2003-12(引用请保留此信息)--*/

/*--调用示例

--调用函数实现分级显示
select replicate('-',b.[level]*4)+a.name
from [tb] a,f_id()b
where a.[id]=b.[id]
order by b.sid
--*/
create function f_id()
returns @re table([id] int,[level] int,sid varchar(8000))
as
begin
declare @l int
set @l=0
insert @re select [id],@l,right(10000+[id],4)
from [tb] where [pid]=0
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.[id],@l,b.sid+right(10000+a.[id],4)
from [tb] a,@re b
where a.[pid]=b.[id] and b.[level]=@l-1
end
return
end
go

--调用函数实现分级显示
select replicate('-',b.[level]*4)+a.name
from [tb] a,f_id()b
where a.[id]=b.[id]
order by b.sid
go


--删除测试
drop table [tb]
drop function f_id
go

/*--结果
中国
----北京
----上海
----江苏
--------苏州
------------常熟
--------南京
--------无锡
美国
----纽约
----旧金山
加拿大

--*/

内容概要:本文系统研究了基于粒子群算法(PSO)的微网优化调度方法,并重点引入需求响应机制以提升微电网运行的经济性与可靠性。研究构建了一个综合考虑风能、光伏、储能、柴油发电机、燃气轮机等多种分布式能源的微电网模型,建立了以最小化系统综合运行成本为目标,涵盖燃料成本、环境污染惩罚、购售电费用及需求响应激励成本的多目标优化模型。文中详细阐述了粒子群算法的数学原理及其在求解此类非线性、多维度、带约束的复杂优化问题中的独特优势,通过Matlab编程实现了完整的算法流程与仿真分析,验证了所提方法在有效降低系统运行成本、平抑负荷波动、提高可再生能源消纳率以及增强微网应对需求侧变化灵活性方面的显著效果。; 适合人群:具备一定电力系统、优化理论基础和Matlab编程能力的高校研究生、从事新能源、智能电网及相关领域研究的科研人员,以及致力于微电网规划与运行优化的工程技术人员。; 使用场景及目标:①用于教学与科研,深入理解微网优化调度的核心挑战、数学建模方法及智能优化算法的应用;②为实际微电网项目的规划设计、运行策略制定提供可靠的算法工具和技术参考;③作为智能优化算法(如PSO)在综合能源系统中成功应用的典型案例,推动先进算法在能源互联网领域的研究与实践。; 阅读建议:建议读者在学习过程中,务必结合文中的Matlab代码进行上机实践,重点关注目标函数的设计逻辑、各类物理约束(如功率平衡、设备出力、储能容量等)的数学表达,以及PSO算法中关键参数(如种群规模、惯性权重、学习因子)的设置与调优技巧,可通过改变参数或扩展模型(如加入更多能源形式或转化为多目标优化)来加深理解和掌握。
2026年,法学硕士工程师的实际收入是多少?每天使用人工智能工具会给你带来更多回报还是让你付出代价?5000个人工智能和数据科学角色,解码。 经典的数据科学薪酬数据集在人工智能工具时代之前就停止了。这是为它而建的:27列,涵盖职位、经验水平、就业类型、公司规模、公司所在地、员工居住地、远程比率、行业、教育、主要语言、经验年限、美元工资、股权和奖金、每周工作时间、每日人工智能工具使用和工作时间、证书、面试机会、工作转换、技能提升时间以及自我报告的自动化恐惧分数。5000行,零缺失值——工资真正受到资历、市场、公司规模、行业和远程设置的影响,因此回归模型实际上学到了一些东西。 让你开始的想法: -构建一个薪酬回归模型——哪个特征的权重最大? -远程比率是伴随着薪酬溢价还是减薪? -LLM/AI头衔是否比经典的数据科学家职位更受欢迎,以及赚多少钱? -按国家绘制薪酬图——一旦你控制了资历,市场差距有多大? -谁最害怕自动化?将分析师与ML工程师进行比较。 -在你控制多年的经验后,是否有博士学位溢价? -日常使用人工智能工具是否与满意度、工作时间或薪酬相关?

34,876

社区成员

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

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