`
dengbaoleng
  • 浏览: 1129388 次
文章分类
社区版块
存档分类
最新评论

先进先出的还款处理触发器

 
阅读更多

有 两个表:表1:借支
姓名 日期 借支单号 借支金额 已还金额 未还余额
张三 99-1-1 000001 10000 0 10000
李四 99-1-2 000002 5000 0 5000
张三 99-2-1 000003 6000 0 6000
张三 99-3-1 000004 10000 0 15000
... ... ... ... .. .....
表2:还款
姓名 日期 还款单号 还款金额

当表2 输入单据时,表1 的“已还金额”“未还余额”根据表2的输入“还款金额”做相应更改。即:
表2:还款
姓名 日期 还款单号 还款金额
张三 99-4-3 000001 13000

表1:借支
姓名 日期 借支单号 借支金额 已还金额 未还余额
张三 99-1-1 000001 10000 10000 0
李四 99-1-2 000002 5000 0 5000
张三 99-2-1 000003 6000 3000 3000
张三 99-3-1 000004 10000 0 15000
... ... ... ... .. .....
表2再输入时:
姓名 日期 还款单号 还款金额
张三 99-4-3 000001 13000
张三 99-4-3 000002 8000
表1为:
表1:借支
姓名 日期 借支单号 借支金额 已还金额 未还余额
张三 99-1-1 000001 10000 10000 0
李四 99-1-2 000002 5000 0 5000
张三 99-2-1 000003 6000 6000 0
张三 99-3-1 000004 15000 5000 10000
... ... ... ... .. .....

---------------------------------------------------------------------------

--测试

--测试数据
create table 表1(姓名 varchar(10),日期 datetime,借支单号 char(6),借支金额 int,已还金额 int,未还余额 int)
insert 表1 select '张三','99-1-1','000001',10000,0 ,10000
union all select '李四','99-1-2','000002',5000 ,0 ,5000
union all select '张三','99-2-1','000003',6000 ,0 ,6000
union all select '张三','99-3-1','000004',15000,0 ,15000 --楼主原来这个数据应该不对

create table 表2(姓名 varchar(10),日期 datetime,还款单号 char(6),还款金额 int)
go

--处理的触发器
create trigger tr_insert on 表2
for insert
as
update a set
已还金额=case when (
select sum(case when 已还金额>0 then 未还余额 else 借支金额 end)
from 表1
where 未还余额>0 and 姓名=a.姓名 and 借支单号<=a.借支单号
)>a.未还余额 then b.还款金额-isnull((
select sum(case when 已还金额>0 then 未还余额 else 借支金额 end)
from 表1
where 未还余额>0 and 姓名=a.姓名 and 借支单号<a.></a.>),0)+isnull(a.已还金额,0)
else a.借支金额 end
,未还余额=case when (
select sum(case when 已还金额>0 then 未还余额 else 借支金额 end)
from 表1
where 未还余额>0 and 姓名=a.姓名 and 借支单号<=a.借支单号
)>a.未还余额 then a.借支金额-(b.还款金额-isnull((
select sum(case when 已还金额>0 then 未还余额 else 借支金额 end)
from 表1
where 未还余额>0 and 姓名=a.姓名 and 借支单号<a.></a.>),0)+isnull(a.已还金额,0))
else 0 end
from 表1 a,inserted b
where a.未还余额>0 and a.姓名=b.姓名 and isnull((
select sum(case when 已还金额>0 then 未还余额 else 借支金额 end)
from 表1
where 未还余额>0 and 姓名=a.姓名 and 借支单号<a.></a.>),0)<=b.还款金额
go

--插入记录测试
insert 表2 select '张三','99-4-3','000001',13000
--显示处理结果
select * from 表1
select * from 表2

--插入记录测试
insert 表2 select '张三','99-4-3','000002',8000
--显示处理结果
select * from 表1
select * from 表2
go

--删除测试
drop table 表1,表2

/*--测试结果

--第一次插入记录的更新结果:

姓名 日期 借支单号 借支金额 已还金额 未还余额
------ ------------------------- --------- --------- --------- ----------
张三 1999-01-01 00:00:00.000 000001 10000 10000 0
李四 1999-01-02 00:00:00.000 000002 5000 0 5000
张三 1999-02-01 00:00:00.000 000003 6000 3000 3000
张三 1999-03-01 00:00:00.000 000004 15000 0 15000

(所影响的行数为 4 行)

姓名 日期 还款单号 还款金额
----- -------------------------- --------- -----------
张三 1999-04-03 00:00:00.000 000001 13000

(所影响的行数为 1 行)

--第二次插入的结果:

姓名 日期 借支单号 借支金额 已还金额 未还余额
------ ------------------------------------ --------- -------- --------
张三 1999-01-01 00:00:00.000 000001 10000 10000 0
李四 1999-01-02 00:00:00.000 000002 5000 0 5000
张三 1999-02-01 00:00:00.000 000003 6000 6000 0
张三 1999-03-01 00:00:00.000 000004 15000 5000 10000

(所影响的行数为 4 行)

姓名 日期 还款单号 还款金额
------ ------------------------- ---------- ----------
张三 1999-04-03 00:00:00.000 000001 13000
张三 1999-04-03 00:00:00.000 000002 8000

(所影响的行数为 2 行)

--*/

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics