SQL 怎么利用游标做加法

来源:百度知道 编辑:UC知道 时间:2024/06/17 13:34:25
比如 有一个table

张三 10
张三 20
李四 1
李四 2
李四 3

用游标将10和20相加,1、2、3相加
将最后的结果加入到一个新表中:

张三 30
李四 6

用游标这么做,帮忙写一下相关代码

create table table2
(
id_2 int identity primary key,
name_2 varchar(20) not null,
counts int
一定要游标么。。。

insert into table2 values('张三',10)
insert into table2 values('张三',20)
insert into table2 values('李四',1)
insert into table2 values('李四',2)
insert into table2 values('李四',3)

select name_2 as'姓名',sum(counts)as '次数' from table2 group by name_2

(等下我帮你写一个。。)

我用游标做了。。。这次没错了!

//这是第一个表。。
create table table2
(
id_2 int identity primary key,
name_2 varchar(20) not null,
counts int
)

insert into table2 values('张三',10)
insert into table2 values('张三',20)
insert into table2 values('李四',1)
insert into table2 values('李四',2)
insert into table2 values('李四',3)

//这是结果表
create table newTable
(
id_