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 2000 Forums
 SQL Server Development (2000)
 More Hierarchy Stuff

Author  Topic 

omahanightlife
Starting Member

4 Posts

Posted - 2006-06-07 : 17:24:08
I'm sure you've all seen a million references to this article: [url]http://www.sqlteam.com/item.asp?ItemID=8866[/url]

Well, I've got another question on it. I just started using the lineage method today as opposed to using a temp table and a recursive stored procedure call to pull a hierarchical query. The only problem I have found with it is that I can't seem to figure out how to sort the items that have a common parent alphabetically.

Example:

I have a hierarchy of ad zones for a website that flows like this (short version):

Global
|-North America
|--United States
|---New York
|----Albany
|----New York City
|---Ohio
|---Texas

Everything is showing up in the right groups when I do my query, but I need to be able to display the items at each level alphabetically, rather than in the order in which they were entered. There was another post similar to what I'm asking, but the solution still eludes me: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=44025

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-06-08 : 01:13:27
Table structure?
Go to Top of Page

omahanightlife
Starting Member

4 Posts

Posted - 2006-06-08 : 08:56:38
Virtually the same as the example in the article.

areaID int
areaName varchar(50)
areaParentID int
areaLevel int
areaLineage varchar(255)

If it makes any difference, I'm using SQL Server 2005.
Go to Top of Page

omahanightlife
Starting Member

4 Posts

Posted - 2006-06-08 : 10:52:21
I found myself a solution based on another article that I found here in the forums. The idea on how to make it work was there, but I had to figure out the code to maintain it myself. Basically I went from using Rob's lineage method

/1
/1/2
/1/2/3
/1/2/4
/1/5/10

To using number groups (converted to varchar)

0000
0000 0000
0000 0001
0000 0002
0000 0002 0000

It's a bit more of a pain to maintain it, but I can. Here's the SP I wrote to insert new items. Any suggestions on structure change would be appreciated.


CREATE PROCEDURE dbo.ads_Areas_Insert
@areaParentID int,
@areaName varchar(50),
@result varchar(255) OUTPUT
AS
SET NOCOUNT ON

IF (@areaParentID > 0)
IF EXISTS(SELECT areaID FROM ads_Areas WHERE areaID = @areaParentID)
BEGIN
DECLARE @PL varchar(255), @POS int, @LIN varchar(255), @LVL int

SET @PL = (SELECT areaLineage FROM ads_Areas WHERE areaID = @areaParentID)
SET @POS = (SELECT COUNT(areaID) FROM ads_Areas WHERE areaParentID = @areaParentID AND areaName < @areaName)
SET @LIN = @PL + SPACE(1) + REPLICATE('0',(4 - LEN(CAST(@POS AS varchar)))) + CAST(@POS AS varchar)
SET @LVL = (SELECT (areaLevel + 1) FROM ads_Areas WHERE areaID = @areaParentID)

INSERT INTO ads_Areas (
areaName, areaParentID, areaLevel, areaLineage
) VALUES (
@areaName, @areaParentID, @LVL, @LIN
)

DECLARE @SIB int
DECLARE C1 CURSOR FOR SELECT areaID FROM ads_Areas WHERE ((areaParentID = @areaParentID) AND (areaName > @areaName)) ORDER BY areaName
OPEN C1
FETCH NEXT FROM C1 INTO @SIB
WHILE @@FETCH_STATUS = 0
BEGIN
SET @POS = (@POS + 1)
SET @LIN = @PL + SPACE(1) + REPLICATE('0',(4 - LEN(CAST(@POS AS varchar)))) + CAST(@POS AS varchar)

UPDATE ads_Areas SET areaLineage = @LIN WHERE areaID = @SIB

FETCH NEXT FROM C1 INTO @SIB
END
CLOSE C1
DEALLOCATE C1

SET @result = 'OK'
END
ELSE
SET @result = 'The parent selected does not exist.'
ELSE
IF EXISTS(SELECT areaID FROM ads_Areas WHERE areaLevel = 0)
SET @result = 'You cannot add a new root area.'
ELSE
BEGIN
INSERT INTO ads_Areas (
areaName, areaParentID, areaLevel, areaLineage
) VALUES (
@areaName, 0, 0, '0000'
)

SET @result = 'OK'
END

Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2006-06-08 : 11:00:04
http://www.thesitedoctor.co.uk/SeventhNight/TreeStructs/
Part 3 talks about managing a paths table...

Let me know if you have any specific questions.


Corey

Co-worker on children "...when I have children, I'm going to beat them. Not because their bad, but becuase I think it would be fun ..."
Go to Top of Page

omahanightlife
Starting Member

4 Posts

Posted - 2006-06-08 : 11:46:39
quote:
Originally posted by Seventhnight

http://www.thesitedoctor.co.uk/SeventhNight/TreeStructs/
Part 3 talks about managing a paths table...

Let me know if you have any specific questions.



Ahh....you have a new URL for your Tree Structures. I was looking through old posts and kept seeing the old URL and it was failing. I ended up looking at the web archive to see your old page.
Go to Top of Page
   

- Advertisement -