--将数据插入临时表
select [ID]=indetity(int,1,1)
       ,* 
into #
from 表--产生序号
update #
set B=(select count(1) from # where A=t.A and ID<t.ID)
from # t--回写数据
truncate table 表
insert 表
select A,B from #--删除临时表
drop table #

解决方案 »

  1.   

    declare @A int,@B intupdate 
        T 
    set 
        @B = case when @A=A then @B+1 else 0 end,
        B  = @B,
        @A = A
      

  2.   

    可以用增加一自增列的方式来实现:
    alter table t1 add id int identity(1,1)
    select a,(select count(*) from t1 b where b.a=a.a and b.id>a.id) b from t1 a order by a,b
    alter table t1 drop column id
      

  3.   

    参看此文邹健回复
    http://community.csdn.net/Expert/topic/3313/3313287.xml?temp=.8957178