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
查看全文
相关阅读:
浮动
关于在windows下同时安装两个mysql,并用Navicat工具同时连接
关于Springboot项目打包部署运行,命令行运行jar提示没有主清单属性
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.(个人笔记)
关于IDEA解决默认的javacompile以及Language level自动默认jdk1.5的问题
你真的精通JavaWeb吗?
Java8新特性(个人笔记待续)
Netty(个人笔记待续)
初识网络编程(1)
走进shiro
原文地址:https://www.cnblogs.com/cyz1980/p/918477.html
最新文章
会议总结2021-2-19
javase思维导图
java集合重难点
链表、队列、栈
StringBuffer类和StringBuilder类
包装类
内部类、异常
java流程控制和循环
Java(扩展)
Java五子棋
热门文章
JAVA面向对象初级
重装后的各种/(ㄒoㄒ)/~~
连续数组
[342]4的幂
[810] 黑板异或游戏
1442. 形成两个异或相等数组的三元组数目
树(上半期)
博客blog
201903
定位
Copyright © 2011-2022 走看看