Swap two rows in a table.

  • Thread starter Thread starter Ryan Ternier
  • Start date Start date
R

Ryan Ternier

Hey,

I'm trying to wrap my head around this and it's hurting hard.
I need to swap 2 rows in a table.

This is basically a list system, where a user can click an up arrow, and the
current record gets -1 in priority.
Here's my code so far.


----------------
CREATE procedure spAdjustQuickLink
@intQID1 int
as

declare @intQID2 int

SET @intQID2 =(SELECT UserQuickLinkID
FROM tblUserQuickLink
WHERE LinkOrder = (SELECT LinkOrder
FROM tblUserQuickLink
WHERE UserQuickLInkID = @intQID1) )

if (@intQID1 = 1)
begin
raiserror('Cannot change link order',-1,-1)
return(1)
end


UPDATE tblUserQuickLink
SET LinkOrder = (LinkOrder - 1)
WHERE UserQuickLinkID = @intQID1

UPDATE tblUserQuickLInk
SET LinkOrder = (LinkOrder +1)
WHERE UserQuickLinkID = @intQID2
GO
 
I was trying to kill 2 birds with one stone, and decided to make 2 SP
instead of just 1. It's working 100%.

Thanks anyways!

/RT
 
Back
Top