When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

The SQL Guru Answers your Questions...


Today's question comes from Kurt M.:

While your article at http://4guysfromrolla.com/webtech/sqlguru/q120899-1.shtml answered the basic question of Michael S., I was hoping that you'd go farther into this issue.

I'm interested (and I'll bet Michael S. is, too) in learning if there is a way to apply such recursive joins in such a manner that a single SELECT query will return all the nodes in a tree in their proper order.

If, as in Mike's example, we have:

	PostID
	ParentID

Any record (tree node) can be the parent of multiple records and the child of only one or none. What I (and probably Mike) would like to know is, is it possible to construct a single SQL statement that will return all of these nodes in their proper depth order with their breadth levels. This statement would mimic a recursive loop, whereas your method (and those derived directly from it with more recursive levels) are restricted to a maximum number of recursions.

In my understanding, such a setup is currently not possible within the current SQL language and even using non standard Oracle or SQL Server tools. The only solution I've seen that allows such records to be SELECTED and and returned in their proper depth order with their breadth level depends on the use of one of two types of dot hash columns.

Any thoughts on this issue?

Thanks,

Kurt M.
Programmer/Analyst
Business Systems & Reporting
Dell

Kurt, you asked:

What I (and probably Mike) would like to know is, is it possible to construct a single SQL statement that will return all of these nodes in their proper depth order with their breadth levels.

Probably, but you'd have to be a little tricky about it. Here's what I'm thinking you would need:

1) Multiple recursive joins up to the maximum tree depth you'd like. Of course, there is usually an imposed limit on the number of joins in a query (e.g. SQL6.5 limits you to 16 joins). In reality, there is always a practical limit on the number of joins you can perform. However, if you were really a masochist, you could probably write something that dynamically generates the proper SQL syntax with the correct joins, as long as your tree wasn't too deep.

2) Some way to combine results from the different incarnations of the table into a set of columns that made sense. Possibly with outer joins and COALESCE?

3) Some way to calculate depth on the fly in the query.... hmm, can't think of a solution to this one off the top of my head.

Or you could use the tactics traditional programming languages use: recursion or iteration. A recursive SP (SQL, at least in MS SQLServer does support recursion) could traverse the tree and spit out the results you want. Of course, this would give you multiple resultsets. Or, you could use an iterative approach with a cursor and a stack(temptable).

But all that sounds like a lot of trouble. If you don't mind storing a little extra information (hey, disk space is cheap), why not just store a post's depth and root post ID when it's added to the table? That would effectively flatten the information, while still retaining the proper relationships between posts. You could calculate the post's depth with a cute little recursive proc that you call when you insert the post.

Then, you can do a straight run through the table, ordering by root post ID, and depth. Sure, it's less elegant, but sometimes less elegant == working solution :)

(WARNING, shamless produt plug follows) By the way, I'm on my second Dell Latitude laptop, and it rocks!

Sean


Note From Scott Mitchell, webmaster of 4Guys:
In the Summer of '99 I was an intern at Microsoft. I worked on a project which contained a table which had recursive definitions upon itself. When a person visited a particular web page, the Microsoft ActiveX TreeView Control would be loaded with the top level items appearing. As the user clicked on each node, the tree would be expanded, and that particular node's children would be loaded.

I also wrote a stored procedure that would return the entire tree structure, using a recursive stored procedure, since we were certain that our depth would, for all practical purposes, be less than 5. So, just wanted to let you know that recursive stored procedures can indeed be used to fully iterate through a complex tree structure.


Read Other SQL Guru Questions


Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES