假设表T中有如下数据:字段A 字段B
a     1111111
a     2222222
a     3333333
a     4444444
b     1111111
b     2222222
b     3333333
b     4444444
b     5555555
c     1111111
c     2222222
c     3333333写一个存储过程,读取一条记录并返回A和B的值,然后将这条记录删掉,希望读出的顺序类似这样:
a     1111111
b     1111111
c     1111111
a     2222222
b     2222222
c     3333333
...这个存储过程该如何写呢?谢谢。

解决方案 »

  1.   

    delete tb
    where exists(select 1 from tb t where t.a=tb.a and t.b<tb.b)
      

  2.   

    这个不就是order by 字段B吗?
      

  3.   

    以下两个要哪个?select * from tb order by b , aselect * , px = (select count(1) from tb where a = t.a and b < t.b) + 1 from tb t order by px , a 
      

  4.   

    ---2005
    select * , id=row_number() over(order by b) from tb t order by id , a
      

  5.   

    create proc test
    as
    declare @a char(1),@b varchar(10)
    select top 1 @a=a,@b=b
    from tb
    select @a,@b
    delete from tb where a=@a and b=@b
      

  6.   

    1、select * from tb order by b,a
    2、select a,b from (select row=number() over(partition by b prder by a) K order by row
      

  7.   

    select * from tb order by b , a
      

  8.   

    while (select count(1) from tab) > 0
    begin
     delete tab
     output deleted.*
     where a=(select top 1 a from tab order by b,a)
     and b=(select top 1 b from tab order by b,a)
    end