比如我有一表和数据(下面的都是数值弄字段):id,ryid
1,1
1,37
2,46
3,3
3,47
4,1404
5,5
5,1405
6,6
6,1407
6,1407其中,ID值相同的行数不定,有可能1条,有可能2条。
ryid的值全部唯一。要求:写个存储过程,传入一数值型参数N,将所有数据  以id分组 向前移动N次。比如如果N=1,则果为:
id,ryid
6,1
6,37
1,46
2,3
2,47
3,1404
4,5
4,1405
5,6
5,1407
5,1407谢谢大家。
明天周末了,祝大家周末愉快!

解决方案 »

  1.   


    --> 测试数据:[TB]
    if object_id('[TB]') is not null drop table [TB]
    create table [TB]([id] int,[ryid] int)
    insert [TB]
    select 1,1 union all
    select 1,37 union all
    select 2,46 union all
    select 3,3 union all
    select 3,47 union all
    select 4,1404 union all
    select 5,5 union all
    select 5,1405 union all
    select 6,6 union all
    select 6,1407 union all
    select 6,1407declare @i int
    set @i=1
    select id=case when id-@i<=0 then (select max(id) from TB) else id-@i end,
    ryid 
    from [TB]
    /*
    id          ryid        
    ----------- ----------- 
    6           1
    6           37
    1           46
    2           3
    2           47
    3           1404
    4           5
    4           1405
    5           6
    5           1407
    5           1407(所影响的行数为 11 行)*/
      

  2.   

    declare @i int
    set @i=1
    select  id=(id-@i)+(select max(id) from TB),
    ryid 
    from [TB]
      

  3.   


    顶个鸟啊,错的下面的可以
    --> 测试数据:[TB]
    if object_id('[TB]') is not null drop table [TB]
    create table [TB]([id] int,[ryid] int)
    insert [TB]
    select 1,1 union all
    select 1,37 union all
    select 2,46 union all
    select 3,3 union all
    select 3,47 union all
    select 4,1404 union all
    select 5,5 union all
    select 5,1405 union all
    select 6,6 union all
    select 6,1407 union all
    select 6,1407declare @i int
    set @i=3
    select  id=(id-@i)+(select max(id) from TB),
    ryid 
    from [TB]
    /*
    id          ryid        
    ----------- ----------- 
    4           1
    4           37
    5           46
    6           3
    6           47
    7           1404
    8           5
    8           1405
    9           6
    9           1407
    9           1407(所影响的行数为 11 行)*/
      

  4.   

    NND,这次对了declare @i int
    set @i=3
    select  id=case when (id-@i)<=0 then (select max(id) from TB)+(id-@i) else id-@i end,
    ryid 
    from [TB]/*
    id          ryid        
    ----------- ----------- 
    4           1
    4           37
    5           46
    6           3
    6           47
    1           1404
    2           5
    2           1405
    3           6
    3           1407
    3           1407(所影响的行数为 11 行)*/
      

  5.   

    不好意思,我在给数据的时候犯了一个严重的错误:就是ID 的值不一定是连接的,比如在我给的数据 ID 的值6过后,有可能是28。
    实在对不起。重新给测试数据id,ryid 
    1,1 
    1,37 
    2,46 
    3,3 
    3,47 
    4,1404 
    5,5 
    5,1405 
    6,6 
    6,1407 
    6,1407
    27,777
    27,888
    39,14158
      

  6.   


    DECLARE  @a TABLE
    (
    id INT,
    pid INT 
    )DECLARE @b TABLE
    (
    id INT
    )INSERT @a SELECT 1,0 UNION ALL 
    SELECT 2, 1 UNION ALL 
    SELECT 3, 1 UNION ALL 
    SELECT 4, 0 UNION ALL 
    SELECT 5, 4 UNION ALL 
    SELECT 6, 4 UNION ALL 
    SELECT 7, 4 INSERT @b SELECT 1 UNION ALL 
    SELECT 2 UNION ALL 
    SELECT 5 UNION ALL 
    SELECT 6 SELECT CASE WHEN a.id IS NULL THEN b.id ELSE a.id END AS id  FROM @b b LEFT JOIN @a a ON b.id = a.pid /***result**/
    id
    -----------
    2
    3
    2
    5
    6(5 row(s) affected)
      

  7.   


    --> Test data : @t
    declare @t table ([id] int,[ryid] int)
    insert into @t
    select 6,1 union all
    select 6,37 union all
    select 1,46 union all
    select 2,3 union all
    select 2,47 union all
    select 3,1404 union all
    select 4,5 union all
    select 4,1405 union all
    select 5,6 union all
    select 5,1407 union all
    select 5,1407
    -----------开始 -----------declare @tmp int,@tmp2 int,@n int
    set @n=2--移动次数
    while @n>0
    begin
    select @tmp2 = id from @t
    update @t set id = @tmp,@tmp=@tmp2,@tmp2=id
    set @n=@n-1
    end
    -----------结束 -----------select * from @tid          ryid
    ----------- -----------
    5           1
    5           37
    6           46
    6           3
    1           47
    2           1404
    2           5
    3           1405
    4           6
    4           1407
    5           1407