早教吧作业答案频道 -->其他-->
sql游标如下,某公司成立于2000年,逐年的销售额如下表(累计销售额指自公司成立以来的销售额):年份当年销售额累计销售额200045.23200156.982002
题目详情
sql 游标
如下,某公司成立于2000年,逐年的销售额如下表(累计销售额指自公司成立以来的销售额):
年份 当年销售额 累计销售额
2000 45.23
2001 56.98
2002 58.32
2003 40.67
2004 60.00
2005 45.05
1、 使用游标的方法,计算每年的累计销售额,并填入表中.
这样写有什么不对
declare @uppersale float
declare @totalsale float
declare xiaoshou_cursor scroll cursor
for
select nowsale,totalsale from xiaoshou
open xiaoshou_cursor
fetch Prior from xiaoshou_cursor into @uppersale,@totalsale
while(@@FETCH_STATUS=0)
begin
update xiaoshou set totalsale=nowsale+@uppersale
fetch next from xiaoshou_cursor into @uppersale,@totalsale
end
close xiaoshou_cursor
deallocate xiaoshou_cursor
希望哪位帮我看哈
如下,某公司成立于2000年,逐年的销售额如下表(累计销售额指自公司成立以来的销售额):
年份 当年销售额 累计销售额
2000 45.23
2001 56.98
2002 58.32
2003 40.67
2004 60.00
2005 45.05
1、 使用游标的方法,计算每年的累计销售额,并填入表中.
这样写有什么不对
declare @uppersale float
declare @totalsale float
declare xiaoshou_cursor scroll cursor
for
select nowsale,totalsale from xiaoshou
open xiaoshou_cursor
fetch Prior from xiaoshou_cursor into @uppersale,@totalsale
while(@@FETCH_STATUS=0)
begin
update xiaoshou set totalsale=nowsale+@uppersale
fetch next from xiaoshou_cursor into @uppersale,@totalsale
end
close xiaoshou_cursor
deallocate xiaoshou_cursor
希望哪位帮我看哈
▼优质解答
答案和解析
CREATE TABLE xiaoshou (
saleyear int PRIMARY KEY,
nowsale decimal(6,2),
totalsale decimal(6,2)
);
GO
INSERT INTO xiaoshou(saleyear, nowsale)
SELECT 2000, 45.23 UNION ALL
SELECT 2001, 56.98 UNION ALL
SELECT 2002, 58.32 UNION ALL
SELECT 2003, 40.67 UNION ALL
SELECT 2004, 60.00 UNION ALL
SELECT 2005, 45.05;
GO
BEGIN
declare @saleyear INT;
declare @uppersale decimal(6,2);
declare @totalsale decimal(6,2);
SET @totalsale=0;
-- 定义游标. (方向向前, 可更新, 按时间排序)
declare xiaoshou_cursor cursor for
select saleyear, nowsale from xiaoshou ORDER BY saleyear
FOR UPDATE;
open xiaoshou_cursor;
fetch NEXT from xiaoshou_cursor into @saleyear, @uppersale;
while(@@FETCH_STATUS=0)
begin
-- 递增总金额.
SET @totalsale=@totalsale + @uppersale;
-- 更新数据.
update xiaoshou set totalsale = @totalsale
WHERE
CURRENT OF xiaoshou_cursor;
fetch next from xiaoshou_cursor into @saleyear, @uppersale;
end
close xiaoshou_cursor
deallocate xiaoshou_cursor
END
GO
SELECT * FROM xiaoshou
GO
saleyear nowsale totalsale
----------- -------- ---------
2000 45.23 45.23
2001 56.98 102.21
2002 58.32 160.53
2003 40.67 201.20
2004 60.00 261.20
2005 45.05 306.25
(6 行受影响)
saleyear int PRIMARY KEY,
nowsale decimal(6,2),
totalsale decimal(6,2)
);
GO
INSERT INTO xiaoshou(saleyear, nowsale)
SELECT 2000, 45.23 UNION ALL
SELECT 2001, 56.98 UNION ALL
SELECT 2002, 58.32 UNION ALL
SELECT 2003, 40.67 UNION ALL
SELECT 2004, 60.00 UNION ALL
SELECT 2005, 45.05;
GO
BEGIN
declare @saleyear INT;
declare @uppersale decimal(6,2);
declare @totalsale decimal(6,2);
SET @totalsale=0;
-- 定义游标. (方向向前, 可更新, 按时间排序)
declare xiaoshou_cursor cursor for
select saleyear, nowsale from xiaoshou ORDER BY saleyear
FOR UPDATE;
open xiaoshou_cursor;
fetch NEXT from xiaoshou_cursor into @saleyear, @uppersale;
while(@@FETCH_STATUS=0)
begin
-- 递增总金额.
SET @totalsale=@totalsale + @uppersale;
-- 更新数据.
update xiaoshou set totalsale = @totalsale
WHERE
CURRENT OF xiaoshou_cursor;
fetch next from xiaoshou_cursor into @saleyear, @uppersale;
end
close xiaoshou_cursor
deallocate xiaoshou_cursor
END
GO
SELECT * FROM xiaoshou
GO
saleyear nowsale totalsale
----------- -------- ---------
2000 45.23 45.23
2001 56.98 102.21
2002 58.32 160.53
2003 40.67 201.20
2004 60.00 261.20
2005 45.05 306.25
(6 行受影响)
看了 sql游标如下,某公司成立于...的网友还看了以下:
初2滴一次函数题,(急!谁帮我解)一个三角形的底边长5CM,高H可以任意伸缩(1)写出面积S随H变 2020-05-13 …
社会消费品零售总额与批发零售业零售额一样,都是指售给城乡居民用于生活消费和售 2020-05-21 …
社会消费品零售总额与批发零售业零售额一样,都是指售给城乡居民用于生活消费和售给社会团体用于 2020-05-30 …
26.下列事项中,应缴纳消费税的有()A.酒厂销售自产白酒B.卷烟厂销售自产卷烟C.商场销售外购白 2020-06-19 …
入队列的操作序列设指针变量front表示链式队列的队头指针,指针变量rear表示链式队列的队尾指针 2020-06-22 …
.“低碳生活,绿色出行”,自行车正逐渐成为人们喜爱的交通工具.某运动商城的自行车销售量自2013年 2020-07-12 …
某运动商城的自行车销售量自2013年起逐年增加,据统计,该商城一月份销售自行车64辆,3月份销售了 2020-07-12 …
某运动商城的自行车销售量自2013年起逐月增加,据统计,该商城1月份销售自行车64辆,3月份销售了 2020-08-03 …
下列各项中,按简易办法依4%征收率减半征收增值税的是A.一般纳税人销售自己使用过的物品B.一般纳税人 2020-11-25 …
下列销售行为,应纳增值税的为A销售电力B销售自来水C销售房屋多选下列销售行为,应纳增值税的为A销售电 2020-12-15 …