Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
brad_lucas
Starting Member
2 Posts |
Posted - 2005-11-15 : 01:23:14
|
| Hi all,[edit: re-written to conform to this (http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx)]How do I cycle through the recordset order.CREATE TABLE tblCycle ( cycleID int IDENTITY (1, 1) NOT NULL , cycleName nvarchar (255) NOT NULL )INSERT INTO tblCycle (cycleName) VALUES ("Week One")INSERT INTO tblCycle (cycleName) VALUES ("Week Two")INSERT INTO tblCycle (cycleName) VALUES ("Week Three")INSERT INTO tblCycle (cycleName) VALUES ("Week Four")What I want to do is order the tblCycle data by whatever "cycle" we're currently in. Here's the Stored Proc I've been toying with:CREATE PROCEDURE prc_listCycle( @cycleID int)ASSELECT * FROM tblCycle....(Not very impressive huh!)This is an example of what I hope to get out of it:Week 1:ID | Title------------1 | Week One2 | Week Two3 | Week Three4 | Week FourWeek 2:ID | Title------------2 | Week Two3 | Week Three4 | Week Four1 | Week OneNote that all the records are still displayed, but the order has stepped one. 1 is now last, while 2 is first.I hope this makes sense and that someone can offer any hints or tips on how I might go about doing this? Do I need to add another column to set the order? I really have no idea and any assistance would be greatly appreciated!Many thanksBrad Lucas |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2005-11-15 : 02:20:30
|
| [code]CREATE TABLE Cycle (cycleID INT IDENTITY (1, 1) NOT NULL ,cycleName NVARCHAR (255) NOT NULL )GOINSERT INTO Cycle (cycleName) VALUES ('Week One')INSERT INTO Cycle (cycleName) VALUES ('Week Two')INSERT INTO Cycle (cycleName) VALUES ('Week Three')INSERT INTO Cycle (cycleName) VALUES ('Week Four')GOCREATE PROCEDURE TESTListCycle@cycleID INTASSELECT cycleID, cycleNameFROM Cycle ORDER BY CASE WHEN CycleID < @cycleID THEN CycleId + 1000000 ELSE CycleID ENDGOEXEC TESTListCycle 1EXEC TESTListCycle 2EXEC TESTListCycle 3EXEC TESTListCycle 4GODROP PROCEDURE TESTListCycleGODROP TABLE CycleGO[/code]MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2005-11-15 : 02:25:08
|
Alternative solutionselect cycleID, cycleNamefrom tblCycleorder by (cycleID + 4 - @cycleID) % 4 [KH] |
 |
|
|
brad_lucas
Starting Member
2 Posts |
Posted - 2005-11-15 : 16:35:50
|
Hi guys,Thanks for your assistance. KH - I think I'll go with your option, it looks easier. I would like to confirm something with you though. If the number of Cycles ever increase, do I simply alter the numbers in the Order By: For example:ORDER BY (cycleID + 7 - @cycle) % 7What I was thinking of doing was get the cycle Count automatically like this:CREATE PROCEDURE getCyclesASDeclare @cycle intDeclare @count int--GET CURRENT CYCLESet @cycle = (SELECT TOP 1 cycle FROM tblCurrentCycle ORDER BY id DESC)--COUNT CYCLESSet @count = (SELECT COUNT(*) FROM tblCycles)--DISPLAY CYCLE IN ORDER OF CURRENT CYCLESELECT *FROM tblCategoriesORDER BY (cycleID + @count - @cycle) % @countGO I haven't tested it extensivly yet, but it seems to work. Can you see any major floors in this?RegardsBrad |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2005-11-15 : 19:20:24
|
If you're going to use that method, I would agree it's better to use the variable so you don't have to alter the stored procedure everytime the data changes. That's why I picked a randomly high number.MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2005-11-15 : 22:26:55
|
| Brad,Yes. It will work for any number of cycles[KH] |
 |
|
|
|
|
|
|
|