Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET 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 February 18th, 2004, 01:14 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Ah, nothing like recursive sql.

The principal is going to sort of the same but backwards. In a nut shell, here's what you can do. Let me first disclaim that I'm not a SQL expert so there may be a MUCH easier or at least more efficient way to do this. I'll assume that you want to sort this list by the item name.

You start with a temp table:
ItemID INT, ParentItemID INT, Level INT, ItemName VARCHAR, SortString VARCHAR
(SortString will need to be pretty long, you'll see why)

<sql>
SET @level=0

--Select all the root items from the table (based on whatever criteria deems a item to be "root", like ParentItemID = NULL). Select them like this:
INSERT INTO #itemTree
    SELECT ItemID, ParentItemID, @level, ItemName, ItemName

WHILE @@ROWCOUNT>0
BEGIN
    SET @level=@level+1
    INSERT INTO #itemTree
        SELECT ItemID, ParentItemID, @level, ItemName, SortString+ItemName
        FROM MyItems AS t1 JOIN #itemTree AS t2 ON t1.ParentItemID=t2.ItemID
        WHERE t2.Level=(@level-1)
END

SELECT ItemID, ParentID, Level, ItemName
FROM #itemTree
ORDER BY SortString
</sql>

The key to this is the SortString. You'll end up with a temp table with a Level and SortString column like this...
0 RootItem1
0 RootItem2
1 RootItem1ItemA
1 RootItem1ItemB
1 RootItem2ItemA
1 RootItem2ItemB
2 RootItem1ItemAChild1
2 RootItem1ItemAChild2
2 RootItem1ItemBChild1
2 RootItem1ItemBChild2
2 RootItem2ItemAChild1
2 RootItem2ItemAChild2
2 RootItem2ItemBChild1
2 RootItem2ItemBChild2

Then thru the natural sorting order on the final select, you get:
0 RootItem1
1 RootItem1ItemA
2 RootItem1ItemAChild1
2 RootItem1ItemAChild2
1 RootItem1ItemB
2 RootItem1ItemBChild1
2 RootItem1ItemBChild2
0 RootItem2
1 RootItem2ItemA
2 RootItem2ItemAChild1
2 RootItem2ItemAChild2
1 RootItem2ItemB
2 RootItem2ItemBChild1
2 RootItem2ItemBChild2

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old February 19th, 2004, 11:27 AM
Authorized User
 
Join Date: Sep 2003
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to flyin
Default

ahh yes, there truly is nothing like recursive sql, it brings on the brain pain :D well, peter, once more, looks great, i'm definately gonna try this, i tell ya what this is gonna help out a bunch. There's truly nothing like learning t-sql on the fly! thanks a bunch, i'll give you some feedback on the progress!

Flyin

 
Old February 19th, 2004, 07:05 PM
Authorized User
 
Join Date: Sep 2003
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to flyin
Default

interesting.. Not sure i need that much info returned, all i really need is the similar result as in if the root categoryID = 3 then return back a string of all the subcategories that belong to 3 and all of it's subcategories subcategories and so on and so on.

Example

Politics 3
Bush 12
War 22
Clinton, 49
Lewinski 94

in this list, rootcategory is politics,
bush is child to politics,
war is child to bush,
clinton is is child to politics,
lewinski is child to clinton.

I really just need to be returned

3,12, 22, 49, 94

any more ideas?

 
Old February 19th, 2004, 07:05 PM
Authorized User
 
Join Date: Sep 2003
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to flyin
Default

BTW, do you have a good reference guide that helps you with these stored procs because i don't use them enough due to the fact that i don't know anything about them! thanks

 
Old February 20th, 2004, 08:41 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

No, no good reference, just an old SQL book, the SQL books online, and my imagination.

If you were to modify the SPROC to accept a start point for your tree, you could easily have a single SPROC that would allow you to get the whole tree or any one branch of the tree...

<sql>
CREATE PROCEDURE getItemTree @nStartItem INT
AS
    SET @level=0

    --Select all the root items from the table (based on whatever criteria deems a item to be "root", like ParentItemID = NULL). Select them like this:
    INSERT INTO #itemTree
        SELECT ItemID, ParentItemID, @level, ItemName, ItemName
        WHERE ParentItemID = @nStartItem
        --If you pass NULL and a 'root' item's parent is NULL,
        -- you'll get the whole tree

    WHILE @@ROWCOUNT>0
    BEGIN
        SET @level=@level+1
        INSERT INTO #itemTree
            SELECT ItemID, ParentItemID, @level, ItemName, SortString+ItemName
            FROM MyItems AS t1 JOIN #itemTree AS t2 ON t1.ParentItemID=t2.ItemID
            WHERE t2.Level=(@level-1)
    END

    SELECT ItemID, ParentID, Level, ItemName
    FROM #itemTree
    ORDER BY SortString
GO
</sql>


Peter
------------------------------------------------------
Work smarter, not harder.





Similar Threads
Thread Thread Starter Forum Replies Last Post
There is already an open datareader associated wit yasinirshad ADO.NET 1 August 12th, 2007 03:37 AM
Problem wit struts and spring DR POMPEII Struts 0 August 17th, 2006 09:16 AM
There is already an open DataReader associated wit jayanp ADO.NET 1 July 2nd, 2006 01:10 PM
Datareader NitinJoshi ADO.NET 4 January 31st, 2005 08:34 AM
"There is already an open DataReader " error kaz VS.NET 2002/2003 4 December 17th, 2003 11:43 PM





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