以下事例,使用游标循环表#temptable中数据,然后让当前行和上一行中的argument1 相加 存放到当前行的 argument2 中,比较简单。

--drop table #temptablecreate table #temptable(  argument1 int,  argument2 int,  argument3 datetime)declare @rowcount int,@argument1 int,@argument2 nvarchar(50),@argument3 datetimeset @rowcount=1set @argument1=1set @argument2=0set @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 + 1end--select * from #temptabledeclare @lastargument2 intset @lastargument2=0set @argument2=0declare _cursor cursor for(select argument1 from #temptable)open _cursor;fetch next from _cursor into @argument2 while @@fetch_status = 0begin        update #temptable  set argument2=@argument2+@lastargument2  where current of _cursor    set @lastargument2=@argument2    fetch next from _cursor into @argument2 endclose _cursordeallocate _cursor--select * from #temptable

第一句fetch next from _cursor into @argument2 这句为什么不能放在while循环的第一行,删除第二行呢?我记得自己当时在这里出错了,呵呵。

更多相关文章

  1. SQL Server之JSON 函数详解
  2. MySQL 5.7.9 服务无法启动-“NET HELPMSG 3534”的解决方法
  3. MySQL系列多表连接查询92及99语法示例详解教程
  4. Linux下MYSQL 5.7 找回root密码的问题(亲测可用)
  5. MySQL 什么时候使用INNER JOIN 或 LEFT JOIN
  6. mybatisplus的坑 insert标签insert into select无参数问题的解决
  7. python起点网月票榜字体反爬案例
  8. 《Android开发从零开始》——25.数据存储(4)
  9. Android系统配置数据库注释(settings.db)

随机推荐

  1. Python 为什么推荐蛇形命名法?
  2. Python 为什么会有个奇怪的“...”对象?
  3. 最近学到的几个Excel可视化骚操作,必须分
  4. Python爬虫自动化,帮小姐姐解放双手
  5. 同学,你这简历上没项目啊!
  6. 详解 Python 的二元算术运算,为什么说减法
  7. Python 为什么能支持任意的真值判断?
  8. 一个在交流群里讨论过两轮的问题,答案竟然
  9. Python 函数为什么会默认返回 None?
  10. 【软考高级知识点】第一章.信息化和信息