Re: Website navigation hierarchy with SQL Server 2005



Brian (dotnetdev@xxxxxxxxx) writes:
However I would prefer to the childnode, and then get parents of that
child recursively. Doing that would leave me with a result set that
could add the top level menu items to and have all the data required.
Any help is greatly appreciated.

I couldn't really understand how you wanted the output. However, judging
from the link you posted, you might be looking for something like:

declare @root int;
set @root = 6;


WITH Nav([NodeId],[ParentNodeId], [Text], [Level]) AS
(
SELECT n2.NodeId, n2.ParentNodeId, n2.Text, n2.[Level]
FROM [dbo].[NavigationNode] n1
JOIN NavigationNode n2 ON n1.ParentNodeId = n2.ParentNodeId
WHERE n1.NodeId = @root

UNION ALL

SELECT n2.NodeId, n2.ParentNodeId, n2.Text, n2.[Level]
FROM [dbo].[NavigationNode] n1
JOIN NavigationNode n2 ON n1.ParentNodeId = n2.ParentNodeId
INNER JOIN Nav n3 ON n3.[ParentNodeId] = n1.[NodeId]

)
SELECT DISTINCT *
FROM Nav
UNION ALL
SELECT NodeId, ParentNodeId, Text, [Level]
FROM NavigationNode
WHERE ParentNodeId IS NULL


Maybe not the most elegant, but the hour is late here...



--
Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
.



Relevant Pages

  • Re: Indexed View Crashes ASP.NET App
    ... to the root tables. ... Since you do not include which version of SQL Server you are using, ... The first two are saved with stored procedures, ... Books Online for SQL Server 2005 at ...
    (comp.databases.ms-sqlserver)
  • Website navigation hierarchy with SQL Server 2005
    ... I'm trying to convert some verbose SQL Server 2000 T-SQL code that uses ... declare @root int; ... WHERE [ParentNodeId] = @root ...
    (comp.databases.ms-sqlserver)
  • Re: Query training -- Complex queries
    ... > Update PositionsEOM ... Here you don't have a derived table, but a correlated subquery. ... UPDATE in Books Online. ... is not written for SQL Server. ...
    (microsoft.public.sqlserver.server)
  • Re: Linked Server: How to check if server exists?
    ... The root problem is described in the Books Online topic Batches. ... Rick Byham, SQL Server Books Online ... linked server don't run on those development machines not having that linked ...
    (microsoft.public.sqlserver.connect)
  • Re: STORED PROCEDURE - passing table name as a parameter
    ... T-SQL, is that you get problems if the view definition does not fit into ... will need to query other system tables, for instance syscolumns. ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ... Books Online for SQL Server 2005 at ...
    (comp.databases.ms-sqlserver)