表A保存产品的组成结构关系,假设一个产品 a001 的结构如下,即产品a001由部件A1,D1,E1组成,A1部件又由更小的部件B1,C1组成, E1则由C1,D1,F1组成. |----B1
|------A1---|
a001----| |----C1
|
|------D1
| |----C1
|------E1---|----D1
|----F1 把这个结构存储在表A中,如下: 当然A表还同样存储着许多产品的结构 父项 子项
---------------------
a001 A1
a001 D1
a001 E1
A1 B1
A1 C1
E1 C1
E1 D1
E1 F1
如何写SQL语句才能从这个表中检索出某个产品的结构,形成如下的效果: 层号 部件号(产品号)
--------------------------------
1 a001
2 A1
3 B1
3 C1
2 D1
2 E1
3 C1
3 D1
3 F1
解决方案 »
- DBChart所绑定的TClientDataset类型数据集变化后,DBChart如何更新
- 关于delphi2010 IDE和code界面切换的疑问? 大家进来讨论一下
- 多线程编程入门,请求指导
- 很简单,大学问,大家看看这个Socket 传输数据怎么回事?
- 如何将从数据库查询到的数据加载到comboBox控件中啊?谢谢
- 请教做类似QQ那样的通信工具的基本思路?
- 难住我了!
- 关于ADOQuery的问题
- 请问各位灵通人士,Win XP 是Win98的升级版还是Win 2000 的升级版(为什么没有服务器版)?
- 12333??
- 关于SQL SERVER插入记录的问题
- 哪位有FastReport3序列号?可以告诉我吗,悬尝20分
union
select distinct '2', c from tb_test where p in (select distinct p from tb_test where p Not in (select c from tb_test ))
union
select distinct '3', c from tb_test where c Not in (select p from tb_test )
//以上假设只有三层。更多层的可把第三层以下的语句改为第二层一样即可。
select 1 as "层号",parent_id as "部件号(产品号)"
from test_01
where parent_id='a001'
union
select level+1 "层号",child_id "部件号(产品号)"
from test_01
start with parent_id='a001'
connect by prior child_id=parent_id
其他数据库的不清楚是否支持
shotking(小金_(想找工作,Delphi+数据库,地点在上海))
的sql在sqlserver里提示有错误.各位高手再来看看啊。不够分可以继续加分.只要问题能搞定
单层bom表结构:
fatherid | childid | childnum
----------------------------------------
中 日 1
中 法 2
中 英 3
俄 加 5
俄 西 9
英 俄 2
英 美 7检索函数:CREATE function dbo.f_id(@pcode varchar(15))
returns @re table(childid varchar(15),[level] int,sid varchar(8000),ifyeah varchar(5),num int)
as
begin
declare @l int
set @l=1
insert @re select distinct fatherid,@l,fatherid,'0' as ifyeah,num=1
from singlebom a
where not exists(select * from singlebom where childid=a.fatherid) and fatherid=@pcode
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.childid,@l, ltrim(rTrim(b.sid))+ '>'+a.childid,'1' as ifyeah ,num=a.childnum*b.num
from singlebom a,@re b
where a.fatherid=b.childid and b.[level]=@l-1 and a.childid not in (select fatherid as childid from singlebom )
insert @re select a.childid,@l, ltrim(rTrim(b.sid))+ '>'+a.childid,'0' as ifyeah ,num=a.childnum*b.num
from singlebom a,@re b
where a.fatherid=b.childid and b.[level]=@l-1 and a.childid in (select fatherid as childid from singlebom ) end
return
end调用检索函数:SELECT [曾數] = LEVEL, [貨號] = childid, [是否葉子] = ifyeah, [數量] = num,
[排序] = sid
FROM f_id('中')
ORDER BY sid查询结果:
层数 货号 是否叶子 数量 结构
1 中 0 1 中
2 日 1 1 中 >日
2 法 1 2 中 >法
2 英 0 3 中 >英
3 俄 0 6 中 >英 >俄
4 加 1 30 中 >英 >俄 >加
4 西 1 54 中 >英 >俄 >西
3 美 1 21 中 >英 >美
@parentname varchar(20) --存储过程参数父结点 如a001
as
begin
declare @l int
set @l=1
create table #t (level int,sname varchar(10))
insert into #t select @l ,@parentname
while @@rowcount>0
begin
set @l=@l+1
insert into #t select @l,m.子项 from 表a m,#t n where m.父项=n.sname and n.level=@l-1
end
select * from #t --即为所求
drop table #t
end
父项 varchar(20) , 子项 varchar(20) )
insert into #a
select
'a001' , 'A1' union all select
'a001' , 'D1' union all select
'a001' , 'E1' union all select
'A1' , 'B1' union all select
'A1' , 'C1' union all select
'E1' , 'C1' union all select
'E1' , 'D1' union all select
'E1' , 'F1'--select * from #a--drop table #a
go
create proc test
@parentname varchar(20) --存储过程参数父结点 如a001
as
begin
declare @l int
set @l=1
create table #t (level int,sname varchar(10))
insert into #t select @l ,@parentname
while @@rowcount>0
begin
set @l=@l+1
insert into #t select @l,m.子项 from #a m,#t n where m.父项=n.sname and n.level=@l-1
end
select * from #t --即为所求
drop table #t
end
godrop proc testexec test 'a001'--测试结果:
level sname
----- ---
1 a001
2 A1
2 D1
2 E1
3 B1
3 C1
3 C1
3 D1
3 F1