实现上移的存储过程

--上移存储过程

create proc sp_sort

@id int

as

declare @SortID int  --排序位置

declare @TempSortID int --临时排序位置

declare @TempID int   --临时编号

begin transaction

select @SortID=SortID from [User] where [ID]=@ID --找出想修改顺序的用户的当前当前排序

select @TempSortID=MIN(SortID) from [User] where SortID>@SortID --找出需要修改顺序的上一级顺序的顺序编号

select @TempID=[ID] from [User] where SortID=@TempSortID --找出上一级编号的ID

update [User] set SortID=@SortID where [ID]=@TempID --把上一级编号下调一级

update [User] set SortID=@TempSortID where[ID]=@id --把下一级编号上调一级

if @@ERROR>0 or @@ROWCOUNT<>1

rollback tran

else

commit tran

go

select * from [user]

exec dbo.sp_sort @id=1

select * from [user]

原文地址:https://www.cnblogs.com/bjjjunjie/p/2191282.html