首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求教如何写下面的修改语句

2012-03-28 
求教怎么写下面的修改语句创建表create table test(id int identity(1,1) not null,a int not null,b char

求教怎么写下面的修改语句
创建表
create table test
(  
  id int identity(1,1) not null,
  a int not null,
  b char(1) not null,
)

插入数据
insert into test values (0,'A') 
insert into test values (0,'B')
insert into test values (0,'B')
insert into test values (0,'A')
insert into test values (0,'B')

现在表里面的数据就是
id a b
1 0 A
2 0 B
3 0 B
4 0 A
5 0 B

怎么通过SQL修改语句把表里面的a列数据变成
id a b
1 1 A
4 2 A
2 1 B
3 2 B
5 3 B

当然表里面不止这几条数据,怎么样做循环判断给b列相同的数据标上a列中的数字?

[解决办法]

SQL code
update tset a=(select count(*) from test where b=t.b and id<=t.id)from test tgo 

热点排行