ASP.NET

网站开发

手动排序,指定要修改位置的索引,改到目标位置的索引

--===================================
-- 手动排序
-- 指定要修改位置的索引,改到目标位置的索引
-- 缺点:如果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

posted on 2007-06-18 15:53  三千世界  阅读(299)  评论(0编辑  收藏  举报

导航