国产探花免费观看_亚洲丰满少妇自慰呻吟_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循環的第一行,刪除第二行呢?我記得自己當時在這里出錯了,呵呵。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 磐安县| 微山县| 平遥县| 大兴区| 湖口县| 福清市| 凤翔县| 浮山县| 聂荣县| 安国市| 六安市| 清水县| 北流市| 鹤庆县| 观塘区| 怀集县| 祁门县| 阿城市| 墨竹工卡县| 徐闻县| 新乐市| 德保县| 民乐县| 贡觉县| 乐平市| 武威市| 密山市| 于田县| 扬州市| 揭阳市| 墨竹工卡县| 洪洞县| 无为县| 阿拉善右旗| 梓潼县| 崇左市| 手游| 景德镇市| 阿拉善左旗| 陆良县| 柞水县|