`
ihuashao
  • 浏览: 4549917 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

SQL BOM

SQL 
阅读更多

BOM展开(按任一父结点展开到最底层)
以下写一个简单的,视具体要求稍做修改即可。
create table 表(levelid int,levelname char(2),parent int)
insertselect 1, 'AA' , 0
union all select 2 , 'BB' , 1
union all select 3 , 'CC' , 1
union all select 4 , 'DD' , 2
union all select 5 , 'EE' , 3
union all select 6 , 'FF', 5

create function bom (@name char(2))
returns @tb table (levelid int,levelname char(2),parent int)
as
begin
insert @tb select levelID,LevelName,parent fromwhere Levelname = @name
while @@rowcount > 0
insert @tb select levelID,LevelName,parent from
where parent in (select levelID from @tb)
and levelID not in (select levelID from @tb)
return
end

select * from dbo.bom('bb')
levelid levelname parent
----------- --------- -----------
2 BB 1
4 DD 2

(所影响的行数为
2 行)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics