April 27, 2009 at 1:03 am
Can we Implement Breadth First Serach in SQL server 2005
April 27, 2009 at 3:44 am
What exactly are the inputs and output?
April 27, 2009 at 5:09 am
IdParentId
10
21
31
41
54
64
76
86
92
102
1110
1210
this is the structure of tree.... 1st column is Id and second one is parentId
I need result set as 1,4,6,7,8,5,2,10,11,12,9,3
April 27, 2009 at 6:49 am
You want the result as a comma-delimited string?
N 56°04'39.16"
E 12°55'05.25"
April 27, 2009 at 9:18 pm
ya but the order should b like i mentioned only...
April 28, 2009 at 1:30 am
Check this...
Declare @tMain Table (mid int, pid int)
Insert into @tMain Values ( 1,0)
Insert into @tMain Values ( 2,1)
Insert into @tMain Values ( 3,1)
Insert into @tMain Values ( 4,1)
Insert into @tMain Values ( 5,4)
Insert into @tMain Values ( 6,4)
Insert into @tMain Values ( 7,6)
Insert into @tMain Values ( 8,6)
Insert into @tMain Values ( 9,2)
Insert into @tMain Values ( 10,2)
Insert into @tMain Values ( 11,10)
Insert into @tMain Values ( 12,10)
Insert into @tMain Values ( 13,12)
Insert into @tMain Values ( 14,12)
Insert into @tMain Values ( 15,14)
Insert into @tMain Values ( 16,14)
Insert into @tMain Values ( 17,16)
--Select 'Insert into @tMain Values ( ' + Cast(mid as varchar(10)) + ',' + Cast(pid as varchar(10)) + ')'
--from ALIEN_MISSILE
Declare @mid int,@pid int, @vMax int
Declare @t1 Table (mmid int)
Set @mid = 1
Set @pid = 0
Declare @cnt int
Set @cnt = 0
while 1=1
begin
Set @cnt = @cnt + 1
Set @vMax = 0
Select @vMax = Max(mid) from @tMain where pid = @mid
if Not Exists(Select * from @t1 where mmid = @mid)
begin
Insert into @t1
Select @mid
end
if Exists(Select * from @tMain where pid = @vMax)
begin
if Not Exists(Select * from @t1 where mmid = @vMax)
begin
Insert into @t1
Select @vMax
end
Set @mid = @vMax
end
else
begin
Insert into @t1
Select mid from @tMain where pid = @mid and mid not in (Select mmid from @t1)
print @mid
Select @mid = pid from @tMain where mid = @mid
while 1=1
begin
Set @cnt = @cnt + 1
if Exists(Select top 1 mid from @tMain where pid = @mid and mid not in (Select mmid from @t1) order by mid)
begin
Select @mid = (Select top 1 mid from @tMain where pid = @mid and mid not in (Select mmid from @t1) order by mid)
BREAK
end
else
Select @mid = pid from @tMain where mid = @mid
print @mid
if Not Exists(Select mid from @tMain where mid not in (Select mmid from @t1 ))
BREAK
end
if Not Exists(Select mid from @tMain where mid not in (Select mmid from @t1 ))
BREAK
end
end
Select * from @t1
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply