当前位置: 代码迷 >> Sql Server >> 请问一个SQL存储过程的面试题
  详细解决方案

请问一个SQL存储过程的面试题

热度:53   发布时间:2016-04-27 18:54:33.0
请教一个SQL存储过程的面试题
有一个表,结构为Tree(Child,Parent),写一个存储过程,[email protected]

------解决方案--------------------

create table tree(child varchar(20),parent varchar(20))
insert tree
select '001 ', '000 ' union all
select '002 ', '001 ' union all
select '003 ', '001 ' union all
select '004 ', '002 ' union all
select '005 ', '002 ' union all
select '006 ', '003 '
go
create proc p(@inputnode varchar(20))
as
declare @i int
set @i=0
declare @re table(child varchar(20),parent varchar(20),level int)
insert into @re
select child,parent,@i from tree where [email protected]
while @@rowcount> 0
begin
set @[email protected]+1
insert into @re
select a.child,a.parent,@i from tree a,@re b where a.parent=b.child and [email protected]
end
select child from @re
go
exec p '001 '
drop table tree
drop proc p
  相关解决方案