create proc proc_tree (@node int)
asbegin
declare @i int
set @i = 0
set nocount on
drop table ##res
create table ##res ( node int, child int, weight int, op int)
insert ##res select *,0 from tree where node = @node
while exists(select * from ##res where op = 0 and child <> 0)
begin
set @i = @i + 1
update ##res set op = @i where op = 0 and child <> 0
insert ##res select a.*,0 from tree a,##res b where a.node = b.child and b.op = @i
endselect * from ##res
set nocount off
endcopy別人的乙個來看看:
--定義儲存過程:
create procedure parts_explosion() //in @current char(25)
result(part_no char(25),structure_level integer,structure_qty decimal(15,6)
,children integer,siblings integer,open_children integer,same_level integer)
begin
declare @current char(25); --存放當前待查產品或零件
declare @level integer; --存放產品層次
declare @structure_qty decimal(15,4); --存放構成數量
select '最初產品編號' into @current; --後台測試用
create table #stack( --中間結果存表
current_item char(25) null,
part_level integer null,
part_qty decimal(15,4) null,
);create table #parts( --最後返回結果存放表
part_no char(25) null,
structure_level integer null,
structure_qty decimal(15,6) null,
children integer null, --存放如果有子級產品.則1.否0.
siblings integer null, --存放是否鄰居產品
open_children integer null, --前台用
same_level integer null, --前台用
);/* select @current='45860020a'*/
insert into #stack values(@current,1,1); --初始中間表
select 1 into @level;
while @level>0 loop
if exists(select* from #stack where part_level=@level) then
select current_item,part_qty into @current,
@structure_qty from #stack
where part_level=@level;
if @level<>1 then
insert into #parts values(@current,@level,@structure_qty,1,1,1,1)
end if
;delete from #stack
where part_level=@level and current_item=@current;
insert into #stack
select part_no,@level+1,part_qty
from product_stru
where product_no=@current;
if @@rowcount>0 then
select @level+1 into @level;
if @@rowcount=1 then
update #parts set same_level=0
where part_no=@current
end if
else
update #parts set children=0
where part_no=@current
end if
else
select @level-1 into @level
end if
end loop
;select part_no,structure_level,structure_qty,children,siblings,open_children,same_level from #parts
end--以下來自鄒鍵的blog
--得到指定id的子id列表
create function f_getchildid(@id int)
returns @re table(id int)
asbegin
insert into @re select id from tb where pid=@id
while @@rowcount>0
insert into @re select a.id
from tb a inner join @re b on a.pid=b.id
where a.id not in(select id from @re)
return
endgo
--得到指定id的父id列表
create function f_getparentid(@id int)
returns @re table(id int)
asbegin
declare @pid int
select @pid=pid from tb where id=@id
while @pid<>0
begin
insert into @re values(@pid)
select @pid=pid from tb where id=@pid
endreturn
endgo
遞迴查詢SQL
lz需要的修改自己為對應的,csdn sqlserve大版主 鄒建 我轉貼並且完善一下 測試資料 create table tb id char 3 pid char 3 name nvarchar 10 insert tb select 001 null 山東省 union all select ...
SQL遞迴查詢
create table t bid int,p bid int insert into t select 1,null union all select 2,1 union all select 3,1 union all select 4,3 union all select 5,4 union...
sql遞迴查詢
有兩個表,a 部門表,儲存部門id 名稱 上級部門 b表 儲存部門員工表 id 姓名 部門id 現在我希望通過一條sql語句取得所有頂級部門下的員工 包含下級部門的員工 drop table dept create table dept dept id varchar 5 up id varchar...