Re: Self-Referencing Query
From: David D Webb (spivey_at_nospam.post.com)
Date: 02/01/05
- Next message: David D Webb: "Re: Backup database without stored procedures"
- Previous message: Andrew J. Kelly: "Re: Backup database without stored procedures"
- In reply to: Aaron Weiker: "Self-Referencing Query"
- Next in thread: Aaron Weiker: "Re: Self-Referencing Query"
- Reply: Aaron Weiker: "Re: Self-Referencing Query"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 31 Jan 2005 20:25:28 -0500
You'll be ggod to go when SQL2005 comes out with CTE's. Joe Celko has a
book on maintaining "normalized" hierarchies. Its complicated, but pretty
fault tolerant. If you can calculate the level of each node ahead of time
and store it in the Menus table, you can build the XML one level at a time.
Obout has a tree control for ASP with an example that uses this approach.
-Dave
"Aaron Weiker" <aaron@sqlprogrammer.org> wrote in message
news:288203632427962739733910@news.microsoft.com...
> Problem: I want to create a menu tree by looking up the table heirarchy
> from a single table. For instance I have one row that points to a previous
> to identify that it belongs in that "folder". Think of it as a file system
> type approach. Ideally I want to represent this as an XML file as I
> already wrote a XSLT file that parses it and creates a nice tree on a web
> page. For an example check out the webpage at http://stats.weiker.org/. It
> uses this XML File to pull the content from.
> http://stats.weiker.org/links.xml
> Here is the DDL for the table.
>
> CREATE TABLE dbo.Menus
> (
> [Id] int not null identity(1,1),
> [ParentId] int,
> [Name] varchar(128) not null,
> [Order] int not null
> CONSTRAINT DF_Menus_Order DEFAULT(0),
> [Url] varchar(1024),
> [CreateDate] smalldatetime not null
> CONSTRAINT DF_Menus_CreateDate DEFAULT(GETDATE()),
> CONSTRAINT PK_Menus PRIMARY KEY CLUSTERED ([Id]),
> CONSTRAINT UK_Menus UNIQUE ([Name], [ParentId]),
> CONSTRAINT FK_Menus_Menus
> FOREIGN KEY ([ParentId])
> REFERENCES dbo.Menus([Id])
> )
>
> The problem I keep running into creating the heirarchy. For example
>
> Folder 1
> --> Item A
> --> Item B
> --> Item C
> -->Folder 2
> -->--> Item A
> -->--> Item F
> Folder 999
> --> Item Z
> --> Item Y
>
> The only solution I currently see is to look through each item and then
> retrieve the items that belong to that and perform recursively. Whenever I
> see this pattern I immediately see my CPU soaring and I break into a cold
> sweat. Maybe not that bad but you get the picture.
> So have I over normalized into this nice little design that I really want
> to work but in reality it is going to cost too much to do?
>
> Or am I just crazy and should just stick to using a pure XML file
> approach? Right now I'm thinking this might be much simpler, but that
> would require accepting defeat.
> --
> Aaron Weiker
> http://aaronweiker.com/
> http://sqlprogrammer.org/
>
>
- Next message: David D Webb: "Re: Backup database without stored procedures"
- Previous message: Andrew J. Kelly: "Re: Backup database without stored procedures"
- In reply to: Aaron Weiker: "Self-Referencing Query"
- Next in thread: Aaron Weiker: "Re: Self-Referencing Query"
- Reply: Aaron Weiker: "Re: Self-Referencing Query"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|