手动排序,指定要修改位置的索引,改到目标位置的索引
--===================================
-- 手动排序
-- 指定要修改位置的索引,改到目标位置的索引
-- 缺点:如果ID号不连续就会排错
--===================================
create proc sp_ManualSort
@StartIndex int,
@EndIndex int
as
if @EndIndex > @StartIndex
begin
update TableName set Sort = @EndIndex where Sort = @StartIndex and ID = Sort
while(@StartIndex < @EndIndex)
begin
update TableName set Sort = @StartIndex where Sort = @StartIndex + 1 and ID = Sort
set @StartIndex = @StartIndex + 1
end
end
else if @EndIndex < @StartIndex
begin
update TableName set Sort = @EndIndex where Sort = @StartIndex and ID = Sort
while(@StartIndex > @EndIndex)
begin
update TableName set Sort = @StartIndex where Sort = @StartIndex - 1 and ID = Sort
set @StartIndex = @StartIndex - 1
end
end
go
-- 手动排序
-- 指定要修改位置的索引,改到目标位置的索引
-- 缺点:如果ID号不连续就会排错
--===================================
create proc sp_ManualSort
@StartIndex int,
@EndIndex int
as
if @EndIndex > @StartIndex
begin
update TableName set Sort = @EndIndex where Sort = @StartIndex and ID = Sort
while(@StartIndex < @EndIndex)
begin
update TableName set Sort = @StartIndex where Sort = @StartIndex + 1 and ID = Sort
set @StartIndex = @StartIndex + 1
end
end
else if @EndIndex < @StartIndex
begin
update TableName set Sort = @EndIndex where Sort = @StartIndex and ID = Sort
while(@StartIndex > @EndIndex)
begin
update TableName set Sort = @StartIndex where Sort = @StartIndex - 1 and ID = Sort
set @StartIndex = @StartIndex - 1
end
end
go