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 |
|
metser
Starting Member
27 Posts |
Posted - 2008-11-07 : 08:20:49
|
| Hello.I am building a web app where a user can choose the records display order. To this end, I have added a "DisplayOrder" column (smallint not null). The user can choose to move the record "down" or "up". As far as I understand, for this to work as expected, I need to get the current record (the one the user wishes to "move") and the previous or next record (depending if the user is moving the record "up" or "down" the list). Then, I need to switch between the current record's DisplayOrder number and the next or previous one (i.e for moving a record "down" the list: a=>a+1, a+1=>a).Can someone help with the correct query/stored procedure?Thank you all in advance. |
|
|
NeilG
Aged Yak Warrior
530 Posts |
Posted - 2008-11-07 : 08:47:30
|
| not something i ever use really but have you looked at cursors there is the option on scroll somewhere in the syntaxhttp://msdn.microsoft.com/en-us/library/ms180169.aspx |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-11-07 : 09:03:29
|
[code]-- Prepare sample dataDECLARE @Sample TABLE ( Item VARCHAR(10), SortOrder INT )INSERT @SampleSELECT 'SQLTeam', 150 UNION ALLSELECT 'Peso', 30 UNION ALLSELECT 'metser', 50001-- Prepare user supplied parametersDECLARE @Item VARCHAR(10), @Direction VARCHAR(4)SELECT @Item = 'SQLTeam', @Direction = 'down'-- Display initial stateSELECT *FROM @SampleORDER BY SortOrder-- Do the movementDECLARE @Place INTUPDATE sSET s.SortOrder = s.recID, @Place = CASE WHEN Item = @Item THEN recID ELSE @Place ENDFROM ( SELECT Item, SortOrder, ROW_NUMBER() OVER (ORDER BY SortOrder) AS recID FROM @Sample ) AS sUPDATE @SampleSET SortOrder = CASE WHEN @Direction = 'UP' AND SortOrder = @Place - 1 THEN @Place WHEN @Direction = 'UP' AND SortOrder = @Place THEN @Place - 1 WHEN @Direction = 'DOWN' AND SortOrder = @Place + 1 THEN @Place WHEN @Direction = 'DOWN' AND SortOrder = @Place THEN @Place + 1 ELSE SortOrder ENDWHERE SortOrder IN (@Place - 1, @Place, @Place + 1)-- Display resultSELECT *FROM @SampleORDER BY SortOrder[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
metser
Starting Member
27 Posts |
Posted - 2008-11-07 : 10:28:40
|
| Thank you very much. I will give it a go. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-11-07 : 10:31:29
|
Go crazy. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-11-07 : 10:42:14
|
[code]CREATE PROCEDURE dbo.spChangeDisplayOrder( @ItemID INT, @Direction VARCHAR(4))ASSET NOCOUNT ONDECLARE @Place INTBEGIN TRANSACTIONUPDATE sSET s.DisplayOrder = s.recID, @Place = CASE WHEN ItemID = @ItemID THEN recID ELSE @Place ENDFROM ( SELECT ItemID, DisplayOrder, ROW_NUMBER() OVER (ORDER BY DisplayOrder) AS recID FROM Table1 ) AS sIF @@ERROR <> 0 BEGIN ROLLBACK TRANSACTION RAISERROR('Could not refresh displayorder.', 18, 1) RETURN -100 ENDUPDATE Table1SET DisplayOrder = CASE WHEN @Direction = 'UP' AND DisplayOrder = @Place - 1 THEN @Place WHEN @Direction = 'UP' AND DisplayOrder = @Place THEN @Place - 1 WHEN @Direction = 'DOWN' AND DisplayOrder = @Place + 1 THEN @Place WHEN @Direction = 'DOWN' AND DisplayOrder = @Place THEN @Place + 1 ELSE DisplayOrder ENDWHERE DisplayOrder IN (@Place - 1, @Place, @Place + 1)IF @@ERROR <> 0 BEGIN ROLLBACK TRANSACTION RAISERROR('Could not change displayorder.', 18, 1) RETURN -110 ENDCOMMIT TRANSACTIONRETURN 0[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
metser
Starting Member
27 Posts |
Posted - 2008-11-07 : 11:27:47
|
| Thank you Peso!!! It worked like a charm!!! |
 |
|
|
|
|
|
|
|