在做管理系统时,不可避免会要求对记录进行上下移动. 假如我们有一张表 t_test ,它的字段如下: CREATE TABLE [ dbo ] . [ t_test ] ( [ sysid ] [ bigint ] NOT NULL , [ cname ] [ nvarchar ] ( 50 ) COLLATE Chinese_PRC_CI_AS NULL , [ position ] [ int ] NULL ) 其中的position用来表示记录排列次序。 首先,讨论一下移动一条记录的的SQL语句写法(为简化问题,只写了上移的语句): -- 传入参数ID declare @id bigint set @id = 5 -- 以下为实现代码 declare @pre bigint , @p0 int // Step1:找到比它小的且最大的Position的那条记录 select @pre = sysid, @p0 = position from ( select max (b.position) as m from t_test a,t_test b where a.sysid = @id