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 |
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2011-03-11 : 03:54:45
|
i have a table of products and i allow to change sortorderso now I have say products productid sortorder1 22 13 34 45 5now if they want to move product x at place 5 up then i do update products set sortorder=sortorder-1 where id=5but then product 4 & 5 have the same order what's the easiest way to reorder all of them each time |
|
theboyholty
Posting Yak Master
226 Posts |
Posted - 2011-03-11 : 05:44:27
|
[code]CREATE TABLE #Products (ProductID INT,SortOrder INT)INSERT INTO #Products (ProductID,SortOrder) VALUES (1,3)INSERT INTO #Products (ProductID,SortOrder) VALUES (2,2)INSERT INTO #Products (ProductID,SortOrder) VALUES (3,4)INSERT INTO #Products (ProductID,SortOrder) VALUES (4,1)INSERT INTO #Products (ProductID,SortOrder) VALUES (5,5)SELECT * FROM #products ORDER BY SortOrderDECLARE @ProductID INTDECLARE @PosChange INTSET @ProductID = 1SET @PosChange = -2--CREATE PROCEDURE SPChangeSortOrder (@ProductID INT,@PosChange INT)--ASDECLARE @DuplicateSortOrder INTSELECT @DuplicateSortOrder = SortOrder + @PosChange FROM #Products WHERE ProductID = @ProductIDUPDATE pSET SortOrder = SortOrder - @PosChangeFROM #Products pWHERE SortOrder >= @DuplicateSortOrderAND ProductID <> @ProductIDUPDATE #ProductsSET SortOrder = SortOrder + @PosChangeWHERE ProductID = @ProductIDSELECT * FROM #Products ORDER BY SortOrderdrop table #products[/code]The above code should do the job. You'll have to convert it into a stored procedure but first check it does what you require.Its a bit of a fudge and you wouldn't neccessarily end up with IDs 1 through 5, but they will be in order so that shouldn't matter.You can paste the whole thing into SSMS and run it. It runs off a temp table for testing purposes. Take a look, have a play with entering different @ProductIDs and @PosChange amounts and see what you think.Nick.---------------------------------------------------------------------------------http://www.mannyroadend.co.uk The official unofficial website of Bury Football Club |
 |
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2011-03-11 : 06:24:28
|
thanks :) |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2011-03-11 : 08:19:25
|
Much simpler and copes with multiple simultaneous users too...-- Prepare sample dataDECLARE @Sample TABLE ( ProductID INT, Sortorder INT )-- Populate sample dataINSERT @SampleVALUES (1, 2), (2, 1), (3, 3), (4, 4), (5, 5)-- Display the initial setSELECT *FROM @SampleORDER BY ProductID-- Mimic user supplied parametersDECLARE @ProductID INT = 5, @PosChange INT = -1-- Do the magic;WITH cteSortorder(Sortorder, Peso)AS ( SELECT Sortorder, ROW_NUMBER() OVER (ORDER BY CASE ProductID WHEN @ProductID THEN SortOrder + @PosChange ELSE SortOrder END, CASE ProductID WHEN @ProductID THEN 0 ELSE 1 END ) AS Peso FROM @Sample)UPDATE cteSortorderSET Sortorder = Peso-- Display the final resultSELECT *FROM @SampleORDER BY ProductID N 56°04'39.26"E 12°55'05.63" |
 |
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2012-01-09 : 04:36:43
|
I know this was an old topic but I had to do this again so tried peso version - works great for -1 but it doesn't seem to work if I want to move the item the other waywhat's the solutionDECLARE @Sample TABLE ( ProductID INT, Sortorder INT )-- Populate sample dataINSERT @SampleVALUES (1, 2), (2, 1), (3, 3), (4, 4), (5, 5)-- Display the initial setSELECT *FROM @SampleORDER BY ProductID-- Mimic user supplied parametersDECLARE @ProductID INT = 1, @PosChange INT = +1-- Do the magic;WITH cteSortorder(Sortorder, Peso)AS ( SELECT Sortorder, ROW_NUMBER() OVER (ORDER BY CASE ProductID WHEN @ProductID THEN SortOrder + @PosChange ELSE SortOrder END, CASE ProductID WHEN @ProductID THEN 0 ELSE 1 END ) AS Peso FROM @Sample)UPDATE cteSortorderSET Sortorder = Peso-- Display the final resultSELECT *FROM @SampleORDER BY ProductID |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2012-01-09 : 06:26:54
|
[code]-- Do the magic;WITH cteSortorder(Sortorder, Peso)AS ( SELECT Sortorder, ROW_NUMBER() OVER (ORDER BY CASE ProductID WHEN @ProductID THEN SortOrder + @PosChange ELSE SortOrder END, CASE ProductID WHEN @ProductID THEN 0 ELSE -@PosChange END ) AS Peso FROM @Sample)[/code] N 56°04'39.26"E 12°55'05.63" |
 |
|
|
|
|
|
|