早教吧作业答案频道 -->其他-->
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游标如下,某公司成立于...的网友还看了以下:
这样的:当销售额在100-199时,按销售额的5%提成;当销售额在200-299时,按销售额10% 2020-05-17 …
直线L过两直线3X+4Y-5=0和2X-3Y+8=0额的交点,且与A(2,3),B(-4,5)的距 2020-06-06 …
excelif计算多级提成举例:0——10000销售额提成10%10001——20000销售额提成 2020-06-07 …
“国美商场”销售某品牌汤锅,其成本为每件80元,9月份的销售额为2万元,10月份商场对这种汤锅的售 2020-06-14 …
批发零售额6899,同比增长18.6%其他行业零售额193,增长5.0%求批发零售零售增长额是其他 2020-06-21 …
人民商场销售某保温水瓶,其成本为每件80元,9月份的销售额为2万元,10月份商场对这种保温瓶的售价 2020-06-28 …
请问这道例题是怎么算出来的,例题:2014年3月,丙白酒厂销售白酒200吨,销售额(不含增值税)为 2020-07-01 …
实际应用题本公司有X和Y两种产品,X产品的提成为销售额的0.8%,Y产品的提成为销售额0.4%,现 2020-07-16 …
某企业2011年年销售额为a亿元,2012年年销售额为b亿元,用代数式表示该企业2012年年销售额的 2020-12-21 …
某种图书原定价为每本10元,预计售出总量为1万册,经过市场分析,如果每本价格上涨x%,售出总量将减少 2020-12-31 …