博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
触发器维护已经用编号/未用编号
阅读量:4191 次
发布时间:2019-05-26

本文共 3375 字,大约阅读时间需要 11 分钟。

原帖地址:

table1为初始化数据,table2为已用票据

在table2上写触发器,table2每insert,update,or 批量delete时,实时体现tabel1的'已用票号','已用票数', '结余票号','结余票数'的值.

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

--示例

--示例数据

create table table1(序号 int,类别 varchar(10),起始号 char(7),终止号 char(7),总数 int,已用票号 varchar(8000),已用票数 int,结余票数 int,结余票号 varchar(8000),组合编号 varchar(20))
insert table1 select 1,'A','0000001','0000010',10,NULL,0,10,'0000001-0000010','A-0000001-0000010'
union  all    select 2,'B','0000011','0000020',10,NULL,0,10,'0000011-0000020','B-0000011-0000020'

create table table2(组合编号 varchar(20),类别 varchar(10),票号 char(7))

go

--触发器

create trigger tr_process on table2
for insert,update,delete
as
select id=identity(int,1,1)
 ,a.组合编号,a.票号
 ,b.起始号,b.终止号
 ,已用票号=cast(null as [varchar] (8000))
 ,结余票号=cast(null as [varchar] (8000))
into #t
from table2 a,table1 b
where a.组合编号=b.组合编号
 and (exists(select 1 from inserted where 组合编号=a.组合编号)
  or exists(select 1 from deleted where 组合编号=a.组合编号))
order by a.组合编号,a.票号

declare @组合编号 varchar(20),@票号 int

 ,@已用票号 varchar(8000),@结余票号 varchar(8000)

update #t set

 @已用票号=case
  when 组合编号=@组合编号
  then case
   when 票号=@票号+1
   then case
    when right(@已用票号,1)='-'
    then @已用票号+票号
    else left(@已用票号,len(@已用票号)-7)+票号
    end
   else case
    when right(@已用票号,1)='-'
    then left(@已用票号,len(@已用票号)-1)
    else @已用票号 end+','+票号+'-'
    end
  else 票号+'-'
  end,
 @结余票号=case
  when 组合编号=@组合编号
  then case
   when 票号=@票号+1
   then left(@结余票号,len(@结余票号)-8)
   when right(9999999+票号,7)+'-'=right(@结余票号,8)
   then left(@结余票号,len(@结余票号)-1)+','
   else @结余票号+right(9999999+票号,7)+','
   end+right(10000001+票号,7)+'-'
  else case
   when 起始号=票号
   then ''
   when cast(起始号 as int)+1=票号
   then 起始号+','
   else 起始号+'-'+right(9999999+票号,7)+','
   end+right(10000001+票号,7)+'-'
  end,
 已用票号=@已用票号,
 结余票号=@结余票号,
 @票号=票号,
 @组合编号=组合编号

update a set

 已用票号=case
  when right(b.已用票号,1)='-'
  then left(b.已用票号,len(b.已用票号)-1)
  else b.已用票号
  end,
 结余票号=case
  when b.终止号=b.票号+1
  then left(b.结余票号,len(b.结余票号)-1)
  when len(b.结余票号)=8 and b.终止号
  then ''
  when b.终止号
  then left(b.结余票号,len(b.结余票号)-9)
  else b.结余票号+b.终止号
  end,
 已用票数=c.已用票数,
 结余票数=a.总数-c.已用票数
from table1 a,#t b,(
 select id=max(id),已用票数=count(*)
 from #t
 group by 组合编号
)c where a.组合编号=b.组合编号
 and b.id=c.id

--处理在子表中被全部删除的数据

if exists(select 1 from deleted a where not exists(select 1 from table2 where 组合编号=a.组合编号))
 update a set 已用票号='',已用票数=0,结余票数=a.总数,结余票号=a.起始号+'-'+a.终止号
 from table1 a,(
  select distinct 组合编号 from deleted a
  where not exists(select 1 from table2 where 组合编号=a.组合编号)
 )b where a.组合编号=b.组合编号
go
--插入第1条记录
insert table2 select 'A-0000001-0000010','A','0000001'

--显示结果

select 序号,已用票号,已用票数,结余票数,结余票号,组合编号 from table1
go

--插入第2条记录

insert table2 select 'A-0000001-0000010','A','0000002'

--显示结果

select 序号,已用票号,已用票数,结余票数,结余票号,组合编号 from table1
go

--插入第3条记录

insert table2 select 'A-0000001-0000010','A','0000004'

--显示结果

select 序号,已用票号,已用票数,结余票数,结余票号,组合编号 from table1
go

--插入第4条记录

insert table2 select 'A-0000001-0000010','A','0000003'

--显示结果

select 序号,已用票号,已用票数,结余票数,结余票号,组合编号 from table1
go

--修改记录

update table2 set 组合编号='B-0000011-0000020',票号='0000011'
where 组合编号='A-0000001-0000010' and 票号='0000002'

--显示结果

select 序号,已用票号,已用票数,结余票数,结余票号,组合编号 from table1
go

--批量删除:

delete from table2
where 票号 in ('0000001','0000002','0000011')

--显示结果

select 序号,已用票号,已用票数,结余票数,结余票号,组合编号 from table1
go

--删除测试

drop table table1,table2

/*--结果自己看--*/

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=82207

你可能感兴趣的文章
蚂蚁集团与CCF达成合作成立基金,支持35岁以下青年学者
查看>>
博纳影业集团副总裁黄巍逝世 享年52岁
查看>>
价值12万汽车网上售价1万?老哥拍下后4S店不认账:标错价了
查看>>
新浪微博被罚了,暂停更新微博热搜榜一周
查看>>
“李国庆午餐1小时”12.94万成交!同时对家暴非常自责
查看>>
苹果iPad新品传言来袭:iPad mini终于要换磨具了?
查看>>
外媒:三星电子正与华为商讨芯片代工事宜
查看>>
拼多多首届“非遗购物节”开幕 十一省市“非遗馆”入驻
查看>>
发牌一周年 国内5G发展如何?
查看>>
iPhone 12机模曝光:继续刘海屏、回归直面边框
查看>>
滴滴总裁柳青:祈祷大家能更理性的看待滴滴司机这个群体
查看>>
美团买菜:采购量提升至平时3-4倍 提供无接触配送服务
查看>>
苹果发文谈iPhone SE的核心竞争力,网友:难道不是便宜吗?
查看>>
最大规模线上新基建项目拉开大幕!第127届广交会今天正式开展
查看>>
华为Mate 40手机将于国庆节发售:搭载全新5nm芯片
查看>>
支付宝要给全国人民发100亿,7月1日起!
查看>>
京东方OLED屏幕无缘iPhone 12首批供货,因测试未通过
查看>>
无人出价!贾跃亭所持2210万股乐视网股票首次拍卖流拍
查看>>
优酷掉队,阿里还能“养”多久?
查看>>
东方航空:拟与携程等出资设立三亚国际航空
查看>>