求BOM子阶id和对应的最上层的父阶id的对应关系的存储过程

zhrongr 2015-07-30 05:21:27
我想用sql server写一个BOM子阶id和对应的最上层的父阶id的对应关系的存储过程。

bom表
partID(子阶) parentID(父阶) yl(用量)
2005 1001 1
2006 2007 1
2007 1002 2
4301 3021 2
4301 4001 3
3021 1002 2
4001 1005 1

product表
ID(部件id) name(部件名称)
2005 G
2006 H
2007 I
4301 D
3021 E
4001 F
1001 A
1002 B
1005 C


我想要取所有是子阶的部件id(也就是这些id是存在父阶的),对应的最上层的父阶,
以及用量和下阶部件id的部件名称。下面是我要的结果。请大家帮忙!

partID(子阶) parentID(父阶) yl(用量) name(部件名称)
2005 1001 1 G
2006 1002 2 H
2007 1002 1 I
4301 1002 4 D
3021 1002 2 E
4301 1005 3 D
4001 1005 1 F
...全文
399 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhrongr 2015-12-21
  • 打赏
  • 举报
回复
引用 3 楼 chg198455 的回复:
仁兄,请问你发的dropdownlist下拉无法选中的问题是怎么解决的,跪求解决方法
你好,我刚查了下当时的资料。不管你是否还有该问题,我都要把我当时的解决方法跟你说下,毕竟你问过我。 我当时在选下拉框时,是有条件的。如果单身有数据,我是不让用户选择下拉值的。否则才可以选择。所以当用户click该控件时,我会去判断单身是否有数据,如果没有数据,我让用户选择;有数据,我让它重定向焦点。 document.getElementById("MasterPage_MasterPageContent_dropList1_txt").focus();
zhrongr 2015-12-21
  • 打赏
  • 举报
回复
引用 3 楼 chg198455 的回复:
仁兄,请问你发的dropdownlist下拉无法选中的问题是怎么解决的,跪求解决方法
不好意思。我很少上这个论坛。所以刚刚才看到。这个问题是去年解决的。现在有些忘记了。你问题解决没?
chg198455 2015-09-21
  • 打赏
  • 举报
回复
仁兄,请问你发的dropdownlist下拉无法选中的问题是怎么解决的,跪求解决方法
LongRui888 2015-08-04
  • 打赏
  • 举报
回复
你可以参考一下这个: 【Transact-SQL】BOM按节点排序 http://blog.csdn.net/sqlserverdiscovery/article/details/21619489
CZP98168 2015-08-04
  • 打赏
  • 举报
回复

declare @bom table(
  partid int,
  parentid int,
  yl int
)

declare @product table(
  id int,
  name nvarchar(20)
)

declare @bomtree table(
  partid int,
  parentid int,
  topid int, --顶级id
  ilevel int, --级次
  yl int
)

--级次变量,初始化为1
declare @ilevel int

set @ilevel = 1

--测试数据
insert into @bom
select
  2005,	1001,	1
union
select  
  2006,	2007,	1
union
select  
  2007,	1002,	2
union
select  
  4301,	3021,	2
union
select  
  4301,	4001,	3
union
select  
  3021,	1002,	2
union
select  
  4001,	1005,	1
  
insert into @product
select
  2005,	'G'
union
select  	
  2006,	'H'
union
select  	
  2007,	'I'
union
select  
  4301,	'D'
union
select  
  3021,	'E'
union
select  
  4001,	'F'
union
select  
  1001,	'A'
union
select  
  1002,	'B'
union
select  
  1005,	'C'  
  
--从顶级开始推算
insert into @bomtree
select distinct
    parentid,
    null,
    parentid,
    @ilevel,
    null
from @bom
where parentid not in (
  select partid
  from @bom
)

while exists(
  select top 1 1001
  from @bomtree
  where partid in (
    select
      parentid
    from @bom
  )
  and ilevel = @ilevel
)
begin
  insert into @bomtree
  select distinct
    b.partid,
    b.parentid,
    t.topid,
    @ilevel + 1,
    b.yl
  from @bom b
    inner join @bomtree t
      on t.partid = b.parentid
        and t.ilevel = @ilevel

  set @ilevel = @ilevel + 1
end

select
  b.partid,
  b.topid,
  p.name,
  b.yl
from @bomtree b
  inner join @product p
    on p.id = b.partid
where b.ilevel > 1    
order by topid,ilevel

partid      topid       name                 yl
----------- ----------- -------------------- -----------
2005        1001        G                    1
2007        1002        I                    2
3021        1002        E                    2
2006        1002        H                    1
4301        1002        D                    2
4001        1005        F                    1
4301        1005        D                    3

22,302

社区成员

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

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