数据库上移和下移

列表排序上移和下移,记录备忘

DECLARE @operType INT = 0;--0下移  1上移
DECLARE @id INT = 1;

declare @preSort int;--上一条记录的排序值
declare @preid int; --上一条记录的id值
declare @curSort int;--当前记录的排序值
declare @nextSort int;--下一条记录的排序值
declare @nextid int; --下一条记录的id值
IF @operType = 0
BEGIN
    with TB As (select top 1000 *,row_number() over(order by Sort desc) as rowid from cn9c080.tb_QQ_CommodityTemplete WHERE status <> 2 order by Sort desc)

    select @nextSort=sort,@nextid=id from TB where rowid=(select rowid-1 from TB where id=@id);
    select @curSort=Sort From cn9c080.tb_QQ_CommodityTemplete Where id=@id;

    update cn9c080.tb_QQ_CommodityTemplete set Sort=@nextSort where id=@id;
    update cn9c080.tb_QQ_CommodityTemplete set Sort=@curSort where id=@nextid
END;
ELSE
BEGIN
    with TB As (select top 1000 *,row_number() over(order by Sort desc) as rowid from cn9c080.tb_QQ_CommodityTemplete WHERE status <> 2 order by Sort desc)
    select @preSort=sort,@preid=id from TB where rowid=(select rowid+1 from TB where id=@id);
    select @curSort=Sort From cn9c080.tb_QQ_CommodityTemplete Where id=@id;

    update cn9c080.tb_QQ_CommodityTemplete set Sort=@preSort where id=@id;
    update cn9c080.tb_QQ_CommodityTemplete set SOrt=@curSort where id=@preid
END;

 

转载于:https://www.cnblogs.com/zzgxl/p/10388137.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值