同一张表内、利用变化的上一行数据动态地修改下一行数据(示例Demo)
2007-10-09 18:56
615 查看
--同一张表内、利用变化的上一行数据动态地修改下一行数据(示例Demo)
select identity(int,1,1) as [id],* into # from account order by [month] asc
CREATE TABLE #Temp (
[id] [int] NOT NULL,
[Month] [datetime] NOT NULL ,
[PlanFare] [money] NULL ,
[PutoutFare] [money] NULL ,
[PlanPreFare] [money] NULL ,
[PutoutPreFare] [money] NULL
)
declare @id int
declare m cursor for select [id] from # order by [id] asc
open m
fetch next from m into @id
while (@@fetch_status=0)
begin
insert into #Temp([id],[Month], PlanFare, PutoutFare, PlanPreFare, PutoutPreFare)
select [id],[Month], PlanFare, PutoutFare, PlanPreFare, PutoutPreFare from # where [id]=@id
UPDATE #
SET #.PlanPreFare = (a.PlanFare - a.putoutFare) + (a.PlanPreFare - a.putoutPreFare)
FROM #Temp AS a
WHERE #.[id] = a.[id]+1
--前行作依据,后行来修改(此次的后行,变成下次的前行,依此循环,直至表中的行遍历结束)
truncate table #Temp
fetch next from m into @id
end
close m
deallocate m
UPDATE Account
SET Account.PlanPreFare = a.PlanPreFare
FROM # AS a
WHERE Account.[Month] = a.[Month]
drop table #
drop table #Temp
select identity(int,1,1) as [id],* into # from account order by [month] asc
CREATE TABLE #Temp (
[id] [int] NOT NULL,
[Month] [datetime] NOT NULL ,
[PlanFare] [money] NULL ,
[PutoutFare] [money] NULL ,
[PlanPreFare] [money] NULL ,
[PutoutPreFare] [money] NULL
)
declare @id int
declare m cursor for select [id] from # order by [id] asc
open m
fetch next from m into @id
while (@@fetch_status=0)
begin
insert into #Temp([id],[Month], PlanFare, PutoutFare, PlanPreFare, PutoutPreFare)
select [id],[Month], PlanFare, PutoutFare, PlanPreFare, PutoutPreFare from # where [id]=@id
UPDATE #
SET #.PlanPreFare = (a.PlanFare - a.putoutFare) + (a.PlanPreFare - a.putoutPreFare)
FROM #Temp AS a
WHERE #.[id] = a.[id]+1
--前行作依据,后行来修改(此次的后行,变成下次的前行,依此循环,直至表中的行遍历结束)
truncate table #Temp
fetch next from m into @id
end
close m
deallocate m
UPDATE Account
SET Account.PlanPreFare = a.PlanPreFare
FROM # AS a
WHERE Account.[Month] = a.[Month]
drop table #
drop table #Temp
相关文章推荐
- 同一张表内、利用变化的上一行数据动态地修改下一行数据(示例Demo)
- Oracle 修改一行数据内存主要变化
- jQuery实现的动态文字变化输出效果示例【附演示与demo源码下载】
- jquery动态添加删除一行数据示例
- easyui-datagrid动态修改url,实现datagrid中数据的更新变化
- 利用JQGrid动态删除多行数据,行号(即删除/增加数据)变化的问题
- 利用lr测试客户端发起的动态变化数据
- jquery动态添加删除一行数据示例
- WPF数据修改demo
- Collectionview学习(三)利用故事版动态改变cell内容展示实战demo
- EChats结合PHP,MYSQL,利用ajax动态数据添加。
- 【C#利用后台动态加载数据】Winform“防界面卡死”
- 利用jQuery动态修改URL地址参数和参数值
- 如何获取自增长列(标识列)的ID,并写入另一张表(多对多关系插入数据示例)
- 利用ajax实现页面动态修改
- volley学习笔记(四):Android利用Volley异步加载数据(JSON和图片)完整示例
- Demo of use SQL (Insert logs in system resource usage table)使用sql 的一个示例(插入系统使用日志数据)
- 利用jsonp抓取某网站数据详细示例
- 利用sstream分割txt文件的一行数据
- SliverLight-Binding之动态修改和数据绑定