bom展开

afei929 2008-12-02 09:20:02
BOM表结构如下:
A主件表
=======
id name
1 asf
2 er

B子件表
=======
pid id name qty
1 3 grr 1
1 2 de 1
2 4 aw 1
2 5 li 1

也就是这样的结构。
1 - 3
- 2 - 4
- 5

现要按尾阶展开(结果应该是3,4,5),按多阶展开(结果应该是3,2,4,5),请教MS SQL怎么实现?
...全文
197 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
afei929 2008-12-02
  • 打赏
  • 举报
回复
层次理论上是没有限制的。这是易助ERP的bom表结构,不是我定义的。
flairsky 2008-12-02
  • 打赏
  • 举报
回复
原来写的,你按自己的要求改动下,看是否能达到效果
flairsky 2008-12-02
  • 打赏
  • 举报
回复
给个例子参照一下

create table #t1(id int,code varchar(10))
insert into #t1(id,code) values(1,'A001');
insert into #t1(id,code) values(2,'B001');
insert into #t1(id,code) values(3,'A002');


create table #t2(id int,code varchar(10))
insert into #t2(id,code) values(1,'B001');
insert into #t2(id,code) values(2,'C001');
insert into #t2(id,code) values(3,'B001');
insert into #t2(id,code) values(3,'B002');


declare @code varchar(10)
set @code = 'c001'
create table #t(code varchar(10))
while ((select count(*) from #t2 where code=@code)>0)
begin
insert into #t
select #t1.code from #t1,#t2
where #t2.code=@code and #t1.id=#t2.id
select @code = code from #t
delete #t where code = @code and @code in (select code from #t2)
end
if (select count(*) from #t) > 0
select * from #t
else select @code as code

drop table #t
drop table #t1
drop table #t2

xieyueqing 2008-12-02
  • 打赏
  • 举报
回复
不好写,层次数有限制么?
apple被占用了 2008-12-02
  • 打赏
  • 举报
回复

为什么要定义成这样的表结构啊???

不能变成一个表吗?
A表的ID中有1和2,但这个2确又是1的子项,这样的表结构很奇怪啊。
xieyueqing 2008-12-02
  • 打赏
  • 举报
回复
不好写,你的层次有限制么?不会是无限制的N层吧?
dawugui 2008-12-02
  • 打赏
  • 举报
回复
参考这个自己做吧.

/*
标题:查询指定节点及其所有子节点的函数
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间: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
dawugui 2008-12-02
  • 打赏
  • 举报
回复
树的查询?

22,209

社区成员

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

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