排序

来源:互联网 发布:试用软件赚钱破解 编辑:程序博客网 时间:2024/04/29 06:40
--编写排序的问题,例如 1、2、3、4、5、6这样的排列号--思路:(1)如果1变成5,那么就是1直接变5,2、3、4、5 分别减1,6不变--(2)如果5变成3,那么5直接变成3,1、2不变,3、4减1,6不变--(3)例如:3变成6,那么6是目标值,3是欲成为目标的值--(4)用事务或者存储过程实现--(5)要传的参数分别是:表名,目标值,欲成为目标的值,标识列(例如ID)Create proc [dbo].[pro_woqu]@ID int ,--ID值@targetValue int ,--目标值@wishtargetValue int,--欲成为目标的值@tableName varchar(100)asbegin tranupdate [User] set DisplayOrder=@targetValue where ID=@ID;if(@targetValue<@wishtargetValue)--如果5变成3,那么5直接变成3,1、2不变,3、4减1,6不变begin update [User]set DisplayOrder=DisplayOrder+1 where DisplayOrder>=@targetValue and DisplayOrder<@wishtargetValue and ID!=@ID and ParentID=0endif(@targetValue>@wishtargetValue)--3变成6,那么6是目标值,3是欲成为目标的值beginupdate [User]set DisplayOrder=DisplayOrder-1 where DisplayOrder<=@targetValue and DisplayOrder>@wishtargetValue and ID!=@ID and ParentID=0endif @@error<>0 begin  rollback tranreturn 0endelse begin commit tranreturn 1end

0 0