Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 20th, 2007, 10:15 AM
Authorized User
 
Join Date: Sep 2006
Location: Belgrade, Serbia, Yugoslavia.
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
Reply With Quote
  #2 (permalink)  
Old May 21st, 2007, 09:39 AM
Authorized User
 
Join Date: Oct 2005
Location: , , .
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
Reply With Quote
  #3 (permalink)  
Old May 21st, 2007, 09:57 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
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
Reply With Quote
  #4 (permalink)  
Old May 22nd, 2007, 04:53 AM
Authorized User
 
Join Date: Sep 2006
Location: Belgrade, Serbia, Yugoslavia.
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 11:52 PM.


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