declare @lev int,@liaohao varchar(30)
select @MID='2',@levs=1
declare @t table(物料ID号 varchar(30),序号 int,物料父ID号 varchar(30),levs int)
insert @t select 物料ID号 ,序号,物料父ID号,@levs from BOM where 物料ID号 =@lMID
while @@rowcount<>0
begin
set @levs=@levs+1
insert @t select 物料ID号,序号,物料父ID号,@levs from BOM
where 物料父ID号 in(select 物料ID号 from @t where levs=@levs-1)
end
select * from @t order by levs
select @MID='2',@levs=1
declare @t table(物料ID号 varchar(30),序号 int,物料父ID号 varchar(30),levs int)
insert @t select 物料ID号 ,序号,物料父ID号,@levs from BOM where 物料ID号 =@lMID
while @@rowcount<>0
begin
set @levs=@levs+1
insert @t select 物料ID号,序号,物料父ID号,@levs from BOM
where 物料父ID号 in(select 物料ID号 from @t where levs=@levs-1)
end
select * from @t order by levs
本文介绍了一种使用SQL声明变量和临时表的方式来进行层级查询的方法。通过递归地从物料清单(BOM)表中检索数据,并按层级排序,实现了对产品结构的深度遍历。
378

被折叠的 条评论
为什么被折叠?



