表结构:ID NAME
A  张三
B  李四
C  王五比如我指定移动一行,结果变为
ID NAME
A  李四
B  王五
C  张三如果指定移动两行,结果为
ID NAME
A  王五
B  张三
C  李四依此类推。

解决方案 »

  1.   

    select id1=identity(int,1,1) , * into #test from tbdeclare @i as int
    set @i = 1select NAME from #test where  id1 > @i
    union all
    select NAME from #test where  id1 <= @i
      

  2.   

    select id1=identity(int,1,1) , * into #test from tbdeclare @i as int
    set @i = 1
    select id1=identity(int,1,1) , name into #test1 from 
    (
    select NAME from #test where  id1 > @i
    union all
    select NAME from #test where  id1 <= @i
    ) aselect a.id , b.name from #test a,#test b where a.id1 = b.id1
      

  3.   

    select id1=identity(int,1,1) , * into #test from tbdeclare @i as int
    set @i = 1
    select id1=identity(int,1,1) , name into #test1 from 
    (
    select NAME from #test where  id1 > @i
    union all
    select NAME from #test where  id1 <= @i
    ) aselect a.id , b.name from #test a,#test1(前面那个这里错了.) b where a.id1 = b.id1
      

  4.   

    declare @ta table(ID varchar(10), NAME varchar(20))
    insert @ta
    select 'A',  '李四' union all
    select 'B',  '王五' union all
    select 'C',  '张三'begin TRANSACTION 
    select *,r_id=identity(int,1,1) into # from @ta
    select id=(select id from #  where r_id=a.r_id-1),name=(select [name] from #  where r_id=a.r_id-1) from # a 
    ROLLBACK TRANSACTION(所影响的行数为 3 行)
    (所影响的行数为 3 行)id         name                 
    ---------- -------------------- 
    NULL       NULL
    A          李四
    B          王五(所影响的行数为 3 行)
      

  5.   

    create table tabletest(id char(1),name varchar(10))
    insert into tabletest
    select 'A','张三'
    union all select 'B','李四'
    union all select 'C','王五'select identity(int,1,1) new_id,*
    into newtable
    from tabletestcreate proc up_moverow(@name varchar(10),@rows int)
    as
    declare @oldname varchar(10)
    declare @row_id int
    declare @new_id int
    select @new_id =new_id from newtable where name=@name
    select @row_id = (select new_id from newtable where name=@name)+@rows
    select @oldname = name from newtable where new_id=@row_idupdate newtable
    set name=@name
    from newtable
    where new_id=@row_idupdate newtable
    set name=@oldname
    from newtable
    where new_id=@new_iddrop table newtable,tabletest--正数向下移动,负数向上移动
    exec up_moverow '王五',-2