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
查看全文
相关阅读:
消除QQ表情小游戏
图片排序
自定义字体
随机图片滚动
生成500个0-1000的随机数&&数组查找—小练习
文字搬运
查找 替换
BeginInvoke和EndInvoke方法
MVC HtmlHelper用法大全
30分钟LINQ教程 【转载】
原文地址:https://www.cnblogs.com/cyz1980/p/918477.html
最新文章
linux命令学习笔记(34):du 命令
linux命令学习笔记(33):df 命令
linux命令学习笔记(31): /etc/group文件详解
linux命令学习笔记(29):chgrp命令
linux命令学习笔记(27):linux chmod命令
linux命令学习笔记(32):gzip命令
linux命令学习笔记(28):tar命令
JavaScript RegExp 对象
使用UML进行项目开发
UML实践详细经典教程----用例图、顺序图、状态图、类图、包图、协作图
热门文章
log4j不输出日志错误分析
教你如何将txt复制到excel的各个单元格;并解决科学计数法显示问题及导致的个位数变0问题
格式化MyEclipse代码(java、jsp、js)行的长度@修改java代码字体@修改Properties文件编码方式
eclipse、myeclipse使用常用的小技巧
eclipse、myeclipse使用常用的小技巧
SQL逻辑查询处理顺序特别提醒
SQL逻辑查询处理顺序特别提醒
又是总结,我就说几句
重置样式
DD_belatedPNG,解决 IE6 不支持 PNG-24 绝佳解决方案
Copyright © 2011-2022 走看看