100%结贴率 游标实现BOM多级查询

sadikaqy 2011-05-31 05:18:29
USE db
go
if object_id('#t1') IS NOT NULL
drop table #t1
go

create table #t1
(
parentID varchar(40),
ID varchar(40),
Lever int,
Name varchar(64)
)

第一层:
Insert into #t values(‘2012’,’909’,1,’ 气缸盖总成’)
第二层:
Insert into #t values(‘909’,’929’,2,’ 阀体’)
Insert into #t values(‘909’,’1709’,2,’ 方螺母2’)
第三层:
Insert into #t values(‘929’,’ 1711’,3,’ 阀’)


第一层:
parentID ID lever name
2012 909 1 气缸盖总成

第二层:
parentID ID lever name
909 929 2 阀体
909 1709 2 方螺母2

第三层:
parentID ID lever name
929 1711 3 阀

想要显示如下:
parentID ID lever name
2012 909 1 气缸盖总成
2012 929 2 阀体
2012 1709 2 方螺母2
2012 1711 3 阀
...全文
128 5 打赏 收藏 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
create table tb
(
parentid varchar(40),
id varchar(40),
lever int,
name varchar(64)
)

--第一层:
insert into tb values('2012','909',1,'气缸盖总成')
--第二层:
insert into tb values('909','929',2,'阀体')
insert into tb values('909','1709',2,'方螺母2')
--第三层:
insert into tb values('929','1711',3,'阀')
go

create function f_getid(@id varchar(40))
returns @t table(parentid varchar(40),id varchar(40),lever int,name varchar(64))
as
begin
insert @t select parentid,id,lever,name from tb where id = @id
while @@rowcount<>0
begin
insert @t select b.parentid,a.id,a.lever,a.name from tb a inner join @t b
on a.parentid = b.id and
not exists(select 1 from @t where id = a.id)
end
return
end
go
select * from dbo.f_getid('909')


drop function dbo.f_getid
drop table tb
AcHerat 2011-06-01
  • 打赏
  • 举报
回复

create table tb
(
parentid varchar(40),
id varchar(40),
lever int,
name varchar(64)
)

--第一层:
insert into tb values('2012','909',1,'气缸盖总成')
--第二层:
insert into tb values('909','929',2,'阀体')
insert into tb values('909','1709',2,'方螺母2')
--第三层:
insert into tb values('929','1711',3,'阀')
go

create function f_getid(@id varchar(40))
returns @t table(parentid varchar(40),id varchar(40),lever int,name varchar(64))
as
begin
insert @t select parentid,id,lever,name from tb where id = @id
while @@rowcount<>0
begin
insert @t select b.parentid,a.id,a.lever,a.name from tb a inner join @t b
on a.parentid = b.id and
not exists(select 1 from @t where id = a.id)
end
return
end
go
select * from dbo.f_getid('909')


drop function dbo.f_getid
drop table tb

/*

parentid id lever name
---------------------------------------- ---------------------------------------- ----------- ----------------------------------------------------------------
2012 909 1 气缸盖总成
2012 929 2 阀体
2012 1709 2 方螺母2
2012 1711 3 阀

(4 行受影响)

--小F-- 2011-06-01
  • 打赏
  • 举报
回复
-->Title:Generating test data
-->Author:wufeng4552
-->Date :2009-09-30 08:52:38
set nocount on
if object_id('tb','U')is not null drop table tb
go
create table tb(ID int, ParentID int)
insert into tb select 1,0
insert into tb select 2,1
insert into tb select 3,1
insert into tb select 4,2
insert into tb select 5,3
insert into tb select 6,5
insert into tb select 7,6
-->Title:查找指定節點下的子結點
if object_id('Uf_GetChildID')is not null drop function Uf_GetChildID
go
create function Uf_GetChildID(@ParentID int)
returns @t table(ID int)
as
begin
insert @t select ID from tb where ParentID=@ParentID
while @@rowcount<>0
begin
insert @t select a.ID from tb a inner join @t b
on a.ParentID=b.id and
not exists(select 1 from @t where id=a.id)
end
return
end
go
select * from dbo.Uf_GetChildID(5)
/*
ID
-----------
6
7
*/
-->Title:查找指定節點的所有父結點
if object_id('Uf_GetParentID')is not null drop function Uf_GetParentID
go
create function Uf_GetParentID(@ID int)
returns @t table(ParentID int)
as
begin
insert @t select ParentID from tb where ID=@ID
while @@rowcount!=0
begin
insert @t select a.ParentID from tb a inner join @t b
on a.id=b.ParentID and
not exists(select 1 from @t where ParentID=a.ParentID)
end
return
end
go
select * from dbo.Uf_GetParentID(2)
/*
ParentID
-----------
1
0
*/



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wufeng4552/archive/2009/09/30/4619995.aspx


修改下这个函数就可以了
htl258_Tony 2011-06-01
  • 打赏
  • 举报
回复
if object_id('tempdb..#t') IS NOT NULL
drop table tempdb..#t
go

create table #t
(
parentID varchar(40),
ID varchar(40),
Lever int,
Name varchar(64)
)

--第一层:
Insert into #t values('2012','909',1,'气缸盖总成')
--第二层:
Insert into #t values('909','929',2,'阀体')
Insert into #t values('909','1709',2,'方螺母2')
--第三层:
Insert into #t values('929','1711',3,'阀')

--SELECT * FROM #t

;WITH t AS (
SELECT parent=parentid,* FROM #t WHERE lever=1
UNION ALL
SELECT parent,b.* FROM t a JOIN #t b ON a.id=b.parentID
)
SELECT parentid=parent,id,lever,name FROM t
/*
parentid id lever name
2012 909 1 气缸盖总成
2012 929 2 阀体
2012 1709 2 方螺母2
2012 1711 3 阀
*/
唐诗三百首 2011-06-01
  • 打赏
  • 举报
回复
SQL 2005起,建议用with .. as ..做递归连接即可,如1楼回复.
内容概要:本白皮书系统梳理了中国建筑声学行业的发展现状,重点围绕隔音材料的技术体系、市场格局与十大代表企业展开研究。文章厘清了隔音、吸音与防撞三大功能的概念边界,阐述了声音传播的物理机制及声学工程的材料、构造、施工与验证四层级结构;详细介绍了阻尼隔音板、隔音毡、吸音棉、减震构件、防撞软包等核心产品及其性能指标,并分析了行业标准、合规要求与工程实践中的常见误区;最后通过对广州朗音阁等十家大湾区代表性企业的并列介绍,揭示了行业“综合一体型”“材料制造型”“品牌产品型”等多元发展模式,强调系统交付能力、合规性与环保安全正成为企业核心竞争力。; 适合人群:建筑声学与隔音材料领域的工程采购方、建筑设计单位、总承包单位、行业研究者及政策监管相关人员。; 使用场景及目标:①帮助采购方科学选型,规避“以材料参数替代系统性能”等常见误区;②指导设计单位在公检法办案区、文化娱乐、医疗教育等细分场景中制定合理的声学解决方案;③为行业研究者提供市场格局、技术趋势与企业模式的客观参考;④推动企业提升合规意识与系统交付能力。; 阅读建议:读者应结合自身角色选择重点章节阅读——采购方可聚焦第一章、第三章、第五章、第八章及第九章;设计单位宜通读第一至七章;研究者建议全面研读并关注第二章市场格局与第四章技术趋势;所有读者均需注意本报告不进行企业排名,企业介绍篇幅差异不代表实力优劣,具体决策应以实地考察与合同约定为准。

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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