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

循环写数据,该怎么解决

2012-01-20 
循环写数据读取表A的B项(int)记录,以4条数据为单位,求和之后写入表B中,求代码?例:表AB15-3253-2-8写入表B

循环写数据
读取表A的B项(int)记录,以4条数据为单位,求和之后写入表B中,求代码?
例:
表A
B
1
5
-3
2
5
3
-2
-8
写入表B后为
5
-2


[解决办法]

SQL code
create table A(B int)insert into A values(1) insert into A values(5) insert into A values(-3) insert into A values(2) insert into A values(5) insert into A values(3) insert into A values(-2) insert into A values(-8)create table B(B int)goselect B , id = identity(int,0,1) into tmp from Ainsert into B select 结果 from (select id = id/4 , 结果 = sum(B) from tmp group by id/4) tselect * from Bdrop table A,B,tmp/*B           ----------- 5-2(所影响的行数为 2 行)*/ 

热点排行