Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 20th, 2007, 10:15 AM
Authorized User
 
Join Date: Sep 2006
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via MSN to VerbatimBOT Send a message via Yahoo to VerbatimBOT
Default Problem with table referencing to itself

Hi everyone,
I have a table like this
Code:
CREATE TABLE temp_sectors
(
    sectorID INT NOT NULL PRIMARY KEY,
    sectorName NVARCHAR(300) NOT NULL,
    headSectorID INT NULL FOREIGN KEY REFERENCES temp_sectors(sectorID)
)
Now, how could I get a list of sectors starting from the one who has no headSectorID with the respective data hierarchy?
For example, if I say something like this
Code:
SELECT s.sectorID, s.sectorName, s2.sectorID, s2.sectorName FROM temp_sectors s
INNER JOIN temp_sectors s2 ON s.sectorID = s2.headSectorID
I would get only the first and the second level of hierarchy, and I need it all...

Best regards,
Aleksandar

Programmer/Application developer
Belgrade, Serbia
__________________
Aleksandar Dragosavac
Belgrade, Serbia
 
Old May 21st, 2007, 09:39 AM
Authorized User
 
Join Date: Oct 2005
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One way to do this is to use a recursive function. Here's a couple of links that discusses recursive functions that may be able to help you:

http://www.sql-server-helper.com/fun...tree-path.aspx
http://www.sql-server-helper.com/fun...ode-level.aspx

SQL Server Helper
How well do you know SQL? Find out with the free test assessment from SQL Server Helper!!!
http://www.sql-server-helper.com/free-test/default.aspx

Got a SQL Server Question? Ask us here: http://www.sql-server-helper.com/forums/default.asp
 
Old May 21st, 2007, 09:57 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

You might also try looking for the publication, "Joe Celko's Trees and Hierarchies in SQL for Smarties", which is an entire book on the subject...

Jeff Mason
Custom Apps, Inc.
je.mason@comcast.net
 
Old May 22nd, 2007, 04:53 AM
Authorized User
 
Join Date: Sep 2006
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via MSN to VerbatimBOT Send a message via Yahoo to VerbatimBOT
Default

Thanks a lot!
I'll see if I can find what I need.

Programmer/Application developer
Belgrade, Serbia





Similar Threads
Thread Thread Starter Forum Replies Last Post
referencing html... Jayahar ASP.NET 3.5 Basics 1 September 2nd, 2008 10:41 PM
Referencing a subform scandalous Access 2 March 16th, 2007 07:05 AM
NAMESPACE & REFERENCING PROBLEM WHEN REBUILDING 3Moose BOOK: ASP.NET Website Programming Problem-Design-Solution 4 January 16th, 2005 12:16 PM
Image Referencing Problem Ben Horne HTML Code Clinic 2 October 21st, 2003 09:10 AM
Linked Table Direct Referencing SerranoG Access 2 July 16th, 2003 08:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.