Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 WTF: a sql "stack" in my pants.

Author  Topic 

00kevin
Yak Posting Veteran

78 Posts

Posted - 2007-03-20 : 21:47:10
oh.... I hate my job sometimes...

anyone have any comments for this coder?

verdy nice... yeess?


declare @current int, @maxlvl int, @pageType int
select @current=root_id from pages where page_id=@page_id
select @pageType=page_type from pages where page_id=@current
select @maxlvl=3

declare @level int, @line int
create table #stack (item int, sorting int, page_type int, level int)
insert into #stack values (@current, 0, @pageType, 1)
select @level = 1

declare @tmp table (recidx int identity(1,1), page_id int, level int, PRIMARY KEY (page_id))

while @level > 0
begin
if exists (select * from #stack where level = @level) and @level <= @maxlvl
begin
select top 1 @current=item, @pageType=page_type from #stack where level=@level order by sorting asc

select @line = @current
insert into @tmp (page_id, level) values(@line, @level-1)

delete from #stack where level = @level and item = @current

if @pageType<>4 and @pageType<>5
begin
insert #stack
select page_id, sorting, page_type, @level + 1
from pages_working
where parent_id = @current and link_placement='main' order by sorting

if @@rowcount > 0
select @level = @level + 1
end
end
else
select @level = @level - 1
end -- while
drop table #stack

select
t2.page_id, t2.parent_id, t2.sorting, t2.page_type, t2.file_name, t2.title, t2.link_text,
t2.published_start_date, t2.published_end_date, t2.is_hidden, t2.is_system, t2.channel_name,
t2.channel_permission, t2.disable_collaboration, t2.last_updated_date, t2.status, t2.owner,
t2.title2, t2.link_text2,
t1.level as lvl
from @tmp as t1 left join pages_working as t2 on (t1.page_id=t2.page_id) order by t1.recidx

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-21 : 01:55:19
Some kind of hierarchy algorithm?


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -