如何用SQL語句來實現展BOM(bill of materiel)?

snakiver 2005-04-06 08:37:35
現有一個bom_mstr表,資料如下:

父物料 子物料
A1 B1
A1 B2
A1 B3
B1 C1
B1 C2
C1 D1
C1 D2


請問怎樣用sql 列出表中的物料清單.
...全文
160 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 2005-04-06
  • 打赏
  • 举报
回复
不能
snakiver 2005-04-06
  • 打赏
  • 举报
回复
請問 zjcxc(邹建) ,能不能用一條SQL語句得到如下結果:

物料
----------
B1
B2
B3
C1
C2
D1
D2
snakiver 2005-04-06
  • 打赏
  • 举报
回复
謝謝 zjcxc(邹建),我先看看.
zjcxc 2005-04-06
  • 打赏
  • 举报
回复
--示例数据
create table tb(父物料 varchar(10),子物料 varchar(10))
insert tb select 'A1','B1'
union all select 'A1','B2'
union all select 'A1','B3'
union all select 'B1','C1'
union all select 'B1','C2'
union all select 'C1','D1'
union all select 'C1','D2'
go

--查询自定义函数
create function f_cid(
@物料 varchar(10)
)returns @re table(物料 varchar(10),[level] int)
as
begin
declare @l int
set @l=0
insert @re select 子物料,@l from tb where 父物料=@物料
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.子物料,@l
from tb a,@re b
where a.父物料=b.物料 and b.[level]=@l-1
end
return
end
go

--查询物料A1的物料
select * from f_cid('A1')
go

--删除测试
drop table tb
drop function f_cid

/*--结果
物料 level
---------- -----------
B1 0
B2 0
B3 0
C1 1
C2 1
D1 2
D2 2

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

27,579

社区成员

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

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