Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

sorting a linked list
Hi,
I have some tables which entries the enduser can give a new sequence.
My current implementation is an extra column "OrderID" that keeps track
of the sequence. Endusers will do a lot of CRUD in these tables and its
a pain to keep this sequences up to date.
A linked list would be much more convenient. Is there any chance to
implement an "order by" clause for a linked list?
Here is my sample code I use:

CREATE TABLE #Categories (
[ID] [int] NOT NULL ,
[Name] [nvarchar] (15)   NOT NULL,
OrderID int not null ,
Predecessor int null,
Sucessor int null
)


INSERT into #Categories ([ID], [Name]  ,OrderID,Predecessor
,Sucessor)
values (1,'cat1',1,null,3 )
INSERT into #Categories ([ID], [Name]  ,OrderID,Predecessor
,Sucessor)
values (2,'cat2',3 ,3,4)
INSERT into #Categories ([ID], [Name]  ,OrderID,Predecessor
,Sucessor)
values (3,'cat3',2 ,1,2)
INSERT into #Categories ([ID], [Name]  ,OrderID,Predecessor
,Sucessor)
values (4,'cat4',4 ,2,null)


SELECT * FROM #Categories Order By OrderID

Drop table #Categories

It currently sorts by OrderID but I want to sort by the sequence as
stated by Predesessor and Sucessor.
Triggers and Cursors are not an option. New features of SQLServer 2005
would be ok.

regards
Mathias


Report this thread to moderator Post Follow-up to this message
Old Post
Mathias Fritsch
10-31-05 01:23 AM


Re: sorting a linked list
A linked list?  This is a strange design for a relational database.  Try the
following SQL 2005 CTE:

WITH Categories(ID, Name, OrderID, Sucessor, Sequence) AS
(
SELECT ID, Name, OrderID, Sucessor, 0 AS Sequence
FROM #Categories
WHERE #Categories.Predecessor IS NULL
UNION ALL
SELECT #Categories.ID, #Categories.Name, #Categories.OrderID,
#Categories.Sucessor, Sequence + 1
FROM #Categories
JOIN Categories ON Categories.Sucessor = #Categories.ID
)
SELECT ID, Name, OrderID
FROM Categories
ORDER BY Sequence

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Mathias Fritsch" <mathiasfritsch@gmx.de> wrote in message
news:1130706134.951873.9180@f14g2000cwb.googlegroups.com...
> Hi,
> I have some tables which entries the enduser can give a new sequence.
> My current implementation is an extra column "OrderID" that keeps track
> of the sequence. Endusers will do a lot of CRUD in these tables and its
> a pain to keep this sequences up to date.
> A linked list would be much more convenient. Is there any chance to
> implement an "order by" clause for a linked list?
> Here is my sample code I use:
>
> CREATE TABLE #Categories (
> [ID] [int] NOT NULL ,
> [Name] [nvarchar] (15)   NOT NULL,
> OrderID int not null ,
> Predecessor int null,
> Sucessor int null
> )
>
>
> INSERT into #Categories ([ID], [Name]  ,OrderID,Predecessor
,Sucesso
r)
> values (1,'cat1',1,null,3 )
> INSERT into #Categories ([ID], [Name]  ,OrderID,Predecessor
,Sucesso
r)
> values (2,'cat2',3 ,3,4)
> INSERT into #Categories ([ID], [Name]  ,OrderID,Predecessor
,Sucesso
r)
> values (3,'cat3',2 ,1,2)
> INSERT into #Categories ([ID], [Name]  ,OrderID,Predecessor
,Sucesso
r)
> values (4,'cat4',4 ,2,null)
>
>
> SELECT * FROM #Categories Order By OrderID
>
> Drop table #Categories
>
> It currently sorts by OrderID but I want to sort by the sequence as
> stated by Predesessor and Sucessor.
> Triggers and Cursors are not an option. New features of SQLServer 2005
> would be ok.
>
> regards
> Mathias
>



Report this thread to moderator Post Follow-up to this message
Old Post
Dan Guzman
10-31-05 01:23 AM


Sponsored Links





Last Thread Next Thread
Post New Thread

MS SQL Server archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 03:20 PM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006