BOM在mysql,展开BOM-数据库专栏,SQL Server

原帖地址

http://community.csdn.net/expert/topic/3384/3384941.xml?temp=.6640436

————————————————————————-

–测试数据

create table [dbo].[bom_detail] (

[pkid] [int] ,–identity (1, 1) not null ,

[bom_head_pkid] [int] not null ,

[children_item] [int] not null ,

[qty] [decimal](10, 0) not null

) on [primary]

create table [dbo].[bom_head] (

[pkid] [int] ,–identity (1, 1) not null ,

[master_item] [int] not null ,

[qty] [int] not null ,

[ver] [char] (20) collate chinese_prc_ci_as not null ,

[status] [nvarchar] (10) collate chinese_prc_ci_as not null ,

) on [primary]

create table [dbo].[item] (

[item] [int] ,–identity (1, 1) not null ,

[brand] [nvarchar] (10) ,

[part_no] [nvarchar] (10)

) on [primary]

insert item select 1 ,a ,a1

union  all  select 2 ,b ,aaaaa

union  all  select 3 ,a ,ad

union  all  select 4 ,a ,ss

union  all  select 5 ,c ,123

union  all  select 6 ,c ,aaadsfd

union  all  select 7 ,d ,d22

union  all  select 8 ,c ,dddd512

union  all  select 9 ,a ,aa3223

union  all  select 10,dd,356

insert bom_head select 1,1,1,1,使用中

union  all      select 2,3,1,1,使用中

union  all      select 3,1,1,2,停用

union  all      select 4,6,1,1,使用中

union  all      select 5,8,1,1,使用中

union  all      select 6,2,1,1,使用中

insert bom_detail select 1, 1,2 ,1

union  all        select 2, 1,6 ,2

union  all        select 3, 2,1 ,1

union  all        select 4, 3,4 ,1

union  all        select 5, 3,5 ,1

union  all        select 6, 4,7 ,1

union  all        select 7, 4,8 ,1

union  all        select 8, 5,9 ,1

union  all        select 9, 5,10,1

union  all        select 10,6,6, 1

go

/*–表间关系说明

bom_head表中pkid为一唯一值,同一master_item的status只有一行为”使用中”,别的都为”停用”(注:是同一master_item),通过master_item与表item的item相关联

bom_detail表中通过bom_head_pkid与bom_head表相关联,通过chidern_item与item表的item相关联

–*/

/*–展开bom的说明

item为1的物料的bom组成情况:

到bom_head表中查找master_item=1 and status=使用中

这样可以找到bom_head表中的pkid=1的记录

我可以从bom_detail表中根据bom_head_pkid=1可以得到master_item这个物料需要用到

childern_item分别为2 和 6 的物料;

在bom_head中master_item值为6并且status=使用中

这样可以到到bom_head的pkid为4

然后再到bom_detail中找到bom_head_pkid=4的记录,这样就可以发现master_item为6的物料需要用到childern_item分别为7和8的物料;

在bom_head中master_item值为8并且status=使用中

这样可以到到bom_head的pkid为45

然后再到bom_detail中找到bom_head_pkid=5的记录,这样就可以发现master_item为8的物料需要用到childern_item为9和10的物料;

这样依次类推

最后要得到一个类示树状的结构

如下图所示

第一层  1 brand,part_no

第二层  2 brand,part_no,qty 6 brand,part_no,qty

第三层     7 brand,part_no,qty  8 brand,part_no,qty

第四层        9 brand,part_no,qty   10 brand,part_no,qty

–*/

–展开bom查询的函数

create function f_bom(

@item int

)returns @r table(

item int,

brand nvarchar(10),

part_no nvarchar(10),

qty decimal(10,0), –取自bom_detail

level int, –层次

sid varchar(8000) –排序字段,通过这个来排序,可以体现出树形的层次

)

as

begin

declare @l int

set @l=0

insert @r select @item,brand,part_no,0,@l,right(10000+item,4)

from item

where item=@item

while @@rowcount>0

begin

set @l=@l+1

insert @r select i.item,i.brand,i.part_no,d.qty,@l,r.sid+,+right(10000+i.item,4)

from item i,bom_head h,bom_detail d,@r r

where r.level=@l-1

and r.item=h.master_item

and h.status=使用中

and h.pkid=d.bom_head_pkid

and d.children_item=i.item

end

return

end

go

–调用函数得到查询结果

select 层次=space(level*2)+├─

,item,brand,part_no,qty

from f_bom(1)

order by sid

go

–删除测试

drop table item,bom_head,bom_detail

drop function f_bom

/*–测试结果

层次           item        brand      part_no    qty

————– ———- ———- ———- ——-

├─           1           a          a1         0

├─         2           b          aaaaa      1

├─       6           c          aaadsfd    1

├─     7           d          d22        1

├─     8           c          dddd512    1

├─   9           a          aa3223     1

├─   10          dd         356        1

├─         6           c          aaadsfd    2

├─       7           d          d22        1

├─       8           c          dddd512    1

├─     9           a          aa3223     1

├─     10          dd         356        1

(所影响的行数为 12 行)

–*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值