zoukankan
html css js c++ java
同一张表内、利用变化的上一行数据动态地修改下一行数据(示例Demo)
--
同一张表内、利用变化的上一行数据动态地修改下一行数据(示例Demo)
/**/
/*
CREATE TABLE Account (
[Month] [datetime] NOT NULL Prmary Key,
[PlanFare] [money] NULL ,
[PutoutFare] [money] NULL ,
[PlanPreFare] [money] NULL ,
[PutoutPreFare] [money] NULL
) --Account表生成脚本
*/
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
查看全文
相关阅读:
【python】raise_for_status()抛出requests.HTTPError错误
【python】python打包生成的exe文件运行时提示缺少模块
单点登录原理与简单实现
Java并发之AQS详解
Java更新
各种java面经资源
HashMap与HashTable的区别
Http 与Https
SpringMVC运行原理
Spring
原文地址:https://www.cnblogs.com/cyz1980/p/918477.html
最新文章
头指针 头结点
JavaWeb三层结构---课设02
软件工程课设-----日程管理系统
计算机网络笔记之第一章概述
DNS信息
netcat工具的使用
permeation开篇
短语 ,直接短语,句柄
任务管理器编码详解
汇编语言学习之汇编语言源程序的输入
热门文章
神经网络总结
gdb调试caffe工程
git命令
【python】yield详细用法
【python】生成器generator
【python】装饰器Decorator
【python】PyCharm里面的c、m、F、f、v、p 含义
【python】requests 异常处理
【python】Requests的三种参数请求方式
【python】Requests 库支持RESTFUL的几种方式
Copyright © 2011-2022 走看看