上移下移存储过程
改存储过程只有两个参数,要移动的id和排序的方式。tabelname需要动态的改变!
WG_sysConfig是要改变的哦tabelName,id是排序主键,orderid是排序字段!
本来我是打算写个适合全部数据的存储过程,可总是实现不了 ,热心的博友请帮我看看端代码
USE [WeiGou] GO /****** Object: StoredProcedure [dbo].[pro_moveUpOrDown] Script Date: 07/26/2013 16:10:00 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER procedure [dbo].[pro_moveUpOrDown] @Id as int, --数据标志id @orderType as int --0升序,1降序 As declare @currentOrderId int --当前标志Id的排序id declare @nextId int --下一个数据标志Id declare @nextOrderId int --下一个数据的排序id declare @lastId int --上一个数据的标志Id declare @lastOrderId int --上个数据的排序id declare @temple varchar(10) --操作过程中varvhar 和int之间的转换 declare @maxOrderId int --降序时最大排序Id declare @minOrderId int --升序是最小排序id declare @sql varchar(200) Begin select @currentOrderId=orderId from WG_sysConfig where id=@Id if(@orderType=0) Begin --升序 --if(@currentOrderId=1) -- Begin --当前排序就是1,无需升序 -- End select @minOrderId=MIN(orderId) from WG_sysConfig --查找最小排序id if(@currentOrderId<>@minOrderId) --当前id不等于最小id才有升序资格 Begin --查询上一个数据的Id和orderId select top 1 @lastId=id,@lastOrderId=orderId from Wg_sysConfig where orderId <@currentOrderId order by orderId desc --当前orderid -1 上个+1 update WG_sysConfig set orderId=orderId+1 where id=@lastId update WG_sysConfig set orderId=orderId-1 where id=@Id End End else --降序 Begin select @maxOrderId=Max(orderId) from WG_sysConfig --查找最大排序Id select @maxOrderId --查找最大排序Id if(@currentOrderId<>@maxOrderId) --当前id不等于最大id才有升序资格 Begin --查询上一个数据的Id和orderId select top 1 @nextId=id,@nextOrderId=orderId from Wg_sysConfig where orderId >@currentOrderId order by orderId asc select top 1 @nextId=id,@nextOrderId=orderId from Wg_sysConfig where orderId >@currentOrderId order by orderId asc --当前orderid +1 上个-1 update WG_sysConfig set orderId=orderId-1 where id=@nextId update WG_sysConfig set orderId=orderId+1 where id=@Id End End end
纠结的代码
USE [WeiGou] GO /****** Object: StoredProcedure [dbo].[Proc_MoveUpOrDown] Script Date: 07/26/2013 15:06:55 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ================================================ -- Template generated from Template Explorer using: -- Create Procedure (New Menu).SQL -- -- Use the Specify Values for Template Parameters -- ALTER PROCEDURE [dbo].[Proc_MoveUpOrDown] @keyFiled varchar(20), --主键字段 @keyValue int , --主键值(一般是id,并且设置自动增长) @orderType as int, --0升序,1降序 @tabelName as varchar(20), --表明 @sortField as varchar(20)--排序字段 As declare @currentOrderId int --当前标志Id的排序id declare @nextId int --下一个数据标志Id declare @nextOrderId int --下一个数据的排序id declare @lastId int --上一个数据的标志Id declare @lastOrderId int --上个数据的排序id declare @temple varchar(10) --操作过程中varvhar 和int之间的转换 declare @maxOrderId int --降序时最大排序Id declare @minOrderId int --升序是最小排序id declare @tempSql varchar(200) --中间sql语句,存放最后执行的sql语句 declare @tempField varchar(20) --中间sql语句,存放中间变量 Begin 在这一步就开始纠结, 我就是想选择两个数然后赋值而已,总是实现不了,苦逼啊
--select @currentOrderId=@sortField from WG_sysConfig where @keyFiled=cast(@keyValue as varchar(10)) --选择出当前排序Id set @tempField=('select '+@sortField+' from WG_sysConfg where '+@keyFiled+'='+CAST(@keyValue as varchar(10))) set @currentOrderId=CAST(@tempField as int) if(@orderType=0) Begin --升序 select @minOrderId=MIN(orderId) from WG_sysConfig --查找最小排序id if(@currentOrderId<>@minOrderId) --当前id不等于最小id才有升序资格 Begin --查询上一个数据的Id和orderId -- select top 1 @lastId=@id,@lastOrderId=@sortField from [@tableName] where @sortField <@currentOrderId order by @sortField desc set @lastId=('select top 1 '+@keyFiled+' from '+@tabelName +'where '+@sortField+' < '+@currentOrderId +'order by '+@sortField +'desc') set @lastOrderId=('select top 1 '+@sortField+' from '+@tabelName +'where '+@sortField+' < '+@currentOrderId +'order by '+@sortField +'desc') --当前orderid -1 上个+1 update WG_sysConfig set orderId=orderId+1 where id=@lastId update WG_sysConfig set orderId=orderId-1 where @keyFiled=@keyValue End End else --降序 Begin select @maxOrderId=Max(orderId) from WG_sysConfig --查找最大排序Id select @maxOrderId --查找最大排序Id if(@currentOrderId<>@maxOrderId) --当前id不等于最大id才有升序资格 Begin --查询上一个数据的Id和orderId -- select top 1 @nextId=id,@nextOrderId=orderId from Wg_sysConfig where orderId >@currentOrderId order by orderId asc set @nextId=('select top 1 '+@keyFiled+' from '+@tabelName +'where '+@sortField+' > '+@currentOrderId +'order by '+@sortField +'asc') set @nextOrderId=('select top 1 '+@sortField+' from '+@tabelName +'where '+@sortField+' > '+@currentOrderId +'order by '+@sortField +'asc') --当前orderid +1 上个-1 set @tempSql='update'+@tabelName+' set '+@sortField+'='+@sortField-1+' where '+@keyFiled+'='+@nextId set @tempSql=@tempSql+'update'+@tabelName+' set '+@sortField+'='+@sortField+1+' where '+@keyFiled+'='+@nextId -- update WG_sysConfig set orderId=orderId-1 where id=@nextId -- update WG_sysConfig set orderId=orderId+1 where id=@Id End End end