没有合适的资源?快使用搜索试试~ 我知道了~
以下事例,使用游标循环表#temptable中数据,然后让当前行和上一行中的argument1 相加 存放到当前行的 argument2 中,比较简单。 --drop table #temptable create table #temptable ( argument1 int, argument2 int, argument3 datetime ) declare @rowcount int,@argument1 int,@argument2 nvarchar(50),@argument3 datetime set @rowcount=1 set @argument1=1 s
资源推荐
资源详情
资源评论
sql中循环处理当前行数据和上一行数据相加减中循环处理当前行数据和上一行数据相加减
以下事例,使用游标循环表#temptable中数据,然后让当前行和上一行中的argument1 相加 存放到当前行的 argument2
中,比较简单。
--drop table #temptable
create table #temptable
(
argument1 int,
argument2 int,
argument3 datetime
)
declare @rowcount int,@argument1 int,@argument2 nvarchar(50),@argument3 datetime
set @rowcount=1
set @argument1=1
set @argument2=0
set @argument3=GETDATE()
while(@rowcount<100)
begin
insert into #temptable(argument1,argument2,argument3)
values(@argument1,@argument2,@argument3)
set @argument1=@argument1 + datepart(day,@argument3)
set @argument3=@argument3-1
set @rowcount = @rowcount + 1
end
--select * from #temptable
declare @lastargument2 int
set @lastargument2=0
set @argument2=0
declare _cursor cursor for(select argument1 from #temptable)
open _cursor;
fetch next from _cursor into @argument2
while @@fetch_status = 0
begin
update #temptable
set argument2=@argument2+@lastargument2
where current of _cursor
set @lastargument2=@argument2
fetch next from _cursor into @argument2
end
close _cursor
deallocate _cursor
--select * from #temptable
问一个问题:
第一句fetch next from _cursor into @argument2 这句为什么不能放在while循环的第一行,删除第二行呢?我记得自己当时在
这里出错了,呵呵。
您可能感兴趣的文章您可能感兴趣的文章:sqlite循环批量插入数据采用批处理文件实现SQLServer 循环批处理
资源评论
weixin_38651661
- 粉丝: 6
- 资源: 961
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- C语言基础-C语言编程基础之Leetcode编程题解之第39题组合总和.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第38题外观数列.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第37题解数独.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第36题有效的数独.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第35题搜索插入位置.zip
- index.wxml
- C语言基础-C语言编程基础之Leetcode编程题解之第33题搜索旋转排序数组.zip
- 基于Python实现的手写数字识别系统源码.zip
- 从网页提取禁止转载的文字
- C语言基础-C语言编程基础之Leetcode编程题解之第32题最长有效括号.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功