views:

779

answers:

4

I have implemented a linked list as a self-referencing database table:

CREATE TABLE LinkedList(
    Id bigint NOT NULL,
    ParentId bigint NULL,
    SomeData nvarchar(50) NOT NULL)

where Id is the primary key, and ParentId is the Id of the previous node on the list. The first node has ParentId = NULL.

I now want to SELECT from the table, sorting the rows in the same order they should appear, as nodes on the list.

Eg.: if the table contains the rows

Id      ParentId  SomeData
24971   NULL      0
38324   24971     1
60088   60089     3
60089   38324     2
61039   61497     5
61497   60088     4
109397  109831    7
109831  61039     6

Then sorting it, using the criteria, should result in:

Id      ParentId  SomeData
24971   NULL      0
38324   24971     1
60089   38324     2
60088   60089     3
61497   60088     4
61039   61497     5
109831  61039     6
109397  109831    7

You're supposed to use the SomeData colum as a control, so please don't cheat doing ORDER by SomeData :-)

+7  A: 

In Oracle:

SELECT Id, ParentId, SomeData
FROM (
  SELECT ll.*, level AS lvl
  FROM LinkedList ll
  START WITH
    ParentID IS NULL
  CONNECT BY
    ParentId = PRIOR Id
)
ORDER BY
  lvl

P. S. It's a bad practice to use NULL as ParentID, as it is not searchable by indices. Insert a surrogate root with id of 0 or -1 instead, and use START WITH ParentID = 0.

Quassnoi
That's a very good answer. How do I do the same in SQLServer 2005?
Nuno G
+1, but not for the anti-NULL comment: using 0 or -1 precludes having a foreign key to enforce integrity.
Tony Andrews
You may always keep a surrogate root and keep it in the database. Full scan for a first entry will be too slow if the table contains many records.
Quassnoi
@Quassnoi: You don't need to create a stored procedure for SQL Server versions 2005 and up. They support Common Table Expressions (CTEs) that let you do the same thing that CONNECT BY does.
Tomalak
Yes, CTEs are the way to do it in SQL Server. See my solution below.
Nuno G
To avoid NULL, the start of the list could use its own ID as its parent, so 'START WITH ParentID = Id' becomes the condition.
Jonathan Leffler
+8  A: 

I found a solution for SQLServer, but looks big and much less elegant than Quassnoi's

WITH SortedList (Id, ParentId, SomeData, Level)
AS
(
  SELECT Id, ParentId, SomeData, 0 as Level
    FROM LinkedList
   WHERE ParentId IS NULL
  UNION ALL
  SELECT ll.Id, ll.ParentId, ll.SomeData, Level+1 as Level
    FROM LinkedList ll
   INNER JOIN SortedList as s
      ON ll.ParentId = s.Id
)

SELECT Id, ParentId, SomeData
  FROM SortedList
 ORDER BY Level
Nuno G
Actually, the name SortedList is a bit confusing - that isn't actually sorted - simply recursive... you need to ORDER BY the final SELECT (see my reply)
Marc Gravell
Fair enough, I have added the column Level and a ORDER BY in the final SELECT.
Nuno G
+4  A: 

(edit: d'oh! While I was debugging you found it too!)

In SQL Server:

;WITH cte (Id, ParentId, SomeData, [Level]) AS (
    SELECT Id, ParentId, SomeData, 0
    FROM LinkedList
    WHERE ParentId IS NULL
    UNION ALL
    SELECT ll.Id, ll.ParentId, ll.SomeData, cte.[Level] + 1
    FROM LinkedList ll
    INNER JOIN cte ON ll.ParentID = cte.ID
)
SELECT * FROM cte
ORDER BY [Level]
Marc Gravell
ah - misread the question; I thought you wanted to order it by SomeData; if you want it in linked-order, then [Level] is the way to go
Marc Gravell
A: 

what about

Msg 530, Level 16, State 1, Line 1 The statement terminated. The maximum recursion 100 has been exhausted before statement completion.

?

adolf garlic