国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

sql 循環處理表數據中當前行和上一行中某值相+/-

2024-07-21 02:49:30
字體:
來源:轉載
供稿:網友
sql 循環處理表數據中當前行和上一行中某值相+/-

  曾經,sql中循環處理當前行數據和上一行數據浪費了我不少時間,學會后才發現如此容易,其實學問就是如此,難者不會,會者不難。

  以下事例,使用游標循環表#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循環的第一行,刪除第二行呢?我記得自己當時在這里出錯了,呵呵。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 郸城县| 开平市| 姜堰市| 旺苍县| 中方县| 蒙城县| 吐鲁番市| 左云县| 田东县| 奎屯市| 革吉县| 澄迈县| 深圳市| 蒙山县| 信宜市| 泰州市| 太保市| 昆山市| 古浪县| 仁化县| 西吉县| 石首市| 乌鲁木齐县| 墨玉县| 长岭县| 贵溪市| 丹东市| 汕尾市| 湛江市| 新乡市| 敦煌市| 高青县| 当涂县| 乐安县| 宝山区| 宁河县| 万宁市| 博白县| 玛多县| 长海县| 鸡东县|