*
The moose likes JDBC and the fly likes SQL Recursion woes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "SQL Recursion woes" Watch "SQL Recursion woes" New topic
Author

SQL Recursion woes

Matthew Snow
Ranch Hand

Joined: May 02, 2007
Posts: 82
I have a table that stores tree structures, the tree name and nodes in the tree with their respective parent node. Here's an example:

Tree Name | Node Name | Parent Node Name
-------------------------------------------------------
Business | Group A |
Business | Group Aa | Group A
Design | Group A |
Design | Group Aa | Group A

As you can see, the Node Names are reusable between the trees. I'm trying to recurse through this table to display the structure of each tree. Unfortunately, since the Node Names are reused between the trees, using a Connect By Prior Node Name = Parent Node Name is causing it to skip between the trees as it recurses. What I need to do as I connect to a lower recursion level is to make sure that the Tree Name has not changed so as to make sure it's still recursing the same tree. Here is the SQL that I have, and SQL Developer takes it as a valid SQL:

select TreeName, NodeName
from TreeTable
start with TreeName in (select distinct TreeName from TreeTable)
connect by prior TreeName = TreeName
connect by prior NodeName = ParentNodeName

However this isn't solving the problem. It's still skipping between trees. From what I can tell, even though our Oracle 10g says this is a valid SQL, it's ignoring all but the last Connect By Prior clause. Any help would be appreciated.

P.S. using Start With TreeName = 'Business' does not help as this constraint only applies at the start of the recursion, as the command name implies. Once it goes down to Level 2 and below it starts skipping between the trees.


When you break the big rules, you get a lot of little rules.
Matthew Snow
Ranch Hand

Joined: May 02, 2007
Posts: 82
Using a AND PRIOR element = element will solve the problem.
 
Don't get me started about those stupid light bulbs.
 
subject: SQL Recursion woes
 
Similar Threads
SQLJ == parent displayed as child if the parent contains no children
SQLJ Question== Parent Node is displayed as a child if that parent has no children
SQLJ == Parent node displayed as a child if the parent node contains no children
XML DOM & children nodes
[Wicket] Form Binding Overly Complex?