读书人

批量更新解决办法

发布时间: 2012-05-22 18:18:54 作者: rapoo

批量更新
表A
id Bcount
1 null
2 null
3 null
表B
id Aid
1 2
2 1
3 1

Aid为表A的id,
现在要更新表A的Bcount字段,结果如下:
表A
id Bcount
1 2
2 1
3 null

[解决办法]

SQL code
create table a (id int identity,bcount int)create table b (id int identity,aid int)insert a select null union all select null union all select nullinsert b select 2 union all select 1 union all select 1select * from a /*id    bcount1    NULL2    NULL3    NULL*/select * from b/*id    aid1    22    13    1*/update a set bcount=(select count(1) from b where a.id=b.aid group by b.aid)/*id    bcount1    22    13    NULL*/drop table adrop table b 

读书人网 >SQL Server

热点推荐