Rick Kierner posted on August 26, 2008 17:43

Just ran into a situation where a marketing person needed a list of every page in my project and its URL.  I got this query from a coworker and he graciously let me publish it to my blog.

   1: DECLARE @PageID uniqueidentifier
   2: CREATE TABLE #temp 
   3: (name VARCHAR(1024),
   4: url VARCHAR(1024)
   5: )
   6:  
   7: DECLARE curs CURSOR FOR SELECT ID FROM sf_pagebase
   8: OPEN curs
   9: FETCH NEXT FROM curs INTO @PageID
  10: WHILE @@FETCH_STATUS = 0
  11: BEGIN
  12: declare @url varchar(1024)
  13: declare @ParentID uniqueidentifier
  14: declare @MenuName varchar(1024)
  15:  
  16: SELECT @MenuName = NULL, @url = NULL, @ParentID = null
  17: select @url = '/' + Name, @ParentID = ParentID, @MenuName = MenuName from sf_pagebase pb 
  18: inner join sf_cmspagecontent cpc on pb.id = cpc.id 
  19: where pb.id = @PageID
  20:  
  21: WHILE @ParentID IS NOT NULL
  22: BEGIN
  23:  
  24: SELECT @ParentID = ParentID, @url = '/' + Name + @url, @MenuName = MenuName + ' > ' + @MenuName from sf_pagebase pb
  25: inner join sf_cmspagecontent cpc on pb.id = cpc.id WHERE pb.id = @ParentID
  26: END
  27: select @url = '~' + @url + '.html'
  28: if (@MenuName IS NOT NULL)
  29: INSERT INTO #temp VALUES(@MenuName, @url)
  30:  
  31: FETCH NEXT FROM curs INTO @PageID
  32: END
  33:  
  34: CLOSE curs
  35: DEALLOCATE curs
  36:  
  37: SELECT * FROM #temp
  38: order by url
  39:  
  40: DROP TABLE #temp
Technorati Tags: ,

Posted in:   Tags: ,

Comments


 David
August 26. 2008 17:57
David
There is a little issue with the query and that happens when multiple language versions are available.  This becomes an issue in the nested 'While' statement as it will repeat inner items because of multiple sf_cmspagecontent returns.

There is two solutions to this.  The first one is to simply put a distinct after the select ('SELECT DISTINCT @ParentID = ParentID, @url = '/' + Name + @url, @MenuName = MenuName + ' > ' + @MenuName from sf_pagebase...')
The second solution is to just search on a specific langID ('SELECT @ParentID = ParentID, @url = '/' + Name + @url, @MenuName = MenuName + ' > ' + @MenuName from sf_pagebase inner join sf_cmspagecontent cpc on pb.id = cpc.id WHERE pb.id = @ParentID AND LangID = 127')

The LangID might change based on your project, but I believe that it is the default for english.

no site


August 27. 2008 18:21
Rick
Happy Birthday Dave!

http://www.rickdoes.net/http://www.rickdoes.net/

Comments are closed
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010 Rick.Brain.Flush()