您的位置:首页 > 其它

触发器实现字符串处理及统计

2007-06-29 14:32 260 查看
原帖地址:

http://community.csdn.net/Expert/topic/3269/3269542.xml?temp=8.726138E-02

table1
序号 类别 起始号 终止号 总数 已用票号 已用票数 结余票号 结余票数 组合编号(主键)
1 A 0000001 0000005 5 0000001,0000003 2 0000002,0000004,0000005 3
A-0000001-0000005
2 B 0000006 00000010 5 0000006,0000008 2 0000007,0000009,0000010 3 B-0000006-0000010
table2
组合编号 类别 票号
A-0000001-0000005 A 0000001
A-0000001-0000005 A 0000003
B-0000006-0000010 B 0000006
B-0000006-0000010 B 0000008

希能在table2写触发器更新table1已用票号,已用票数,结余票号,结余票数的字段。

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

--示例

--示例数据
create table table1(序号 int,类别 varchar(10),起始号 char(7),终止号 char(7),总数 int,已用票号 varchar(8000),已用票数 int,结余票号 varchar(8000),结余票数 int,组合编号 varchar(20))
insert table1 select 1,'A','0000001','0000005',5,'',0,'0000001,0000002,0000003,0000004,0000005',5,'A-0000001-0000005'
union all select 2,'B','0000006','0000010',5,'',0,'0000006,0000007,0000006,0000009,0000010',5,'B-0000006-0000010'

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

--处理的触发器
create trigger tr_process on table2
for insert,update,delete
as
declare @组合编号 varchar(20)
,@sta varchar(8000),@nma int
,@stb varchar(8000),@nmb int

--处理被删除的
select d.组合编号,d.票号
,已用票号=','+a.已用票号+',',a.已用票数
,结余票号=a.结余票号,a.结余票数
into #d
from deleted d,table1 a
where d.组合编号=a.组合编号
order by d.组合编号,d.票号

if @@rowcount>0
begin
update #d set
@sta=replace(
case 组合编号 when @组合编号 then @sta else 已用票号 end,
','+票号+',',','),
@nma=case 组合编号 when @组合编号 then @nma-1 else 已用票数-1 end,
@stb=case 组合编号 when @组合编号 then @stb+','
else case 结余票号 when '' then '' else 结余票号+',' end end+票号,
@nmb=case 组合编号 when @组合编号 then @nmb+1 else 结余票数+1 end,
@组合编号=组合编号,
已用票号=@sta,已用票数=@nma,
结余票号=@stb,结余票数=@nmb

update a set 已用票号=case b.已用票数 when 0 then ''
else substring(b.已用票号,2,len(b.已用票号)-2) end
,已用票数=b.已用票数
,结余票号=b.结余票号
,结余票数=b.结余票数
from table1 a,#d b,(
select 组合编号,已用票数=min(已用票数)
from #d
group by 组合编号
)c where a.组合编号=b.组合编号
and c.组合编号=b.组合编号
and c.已用票数=b.已用票数
end

--处理新增的
select i.组合编号,i.票号
,已用票号=a.已用票号,a.已用票数
,结余票号=','+a.结余票号+',',a.结余票数
into #i
from inserted i,table1 a
where i.组合编号=a.组合编号
order by i.组合编号,i.票号

if @@rowcount>0
begin
set @组合编号=null
update #i set
@sta=case 组合编号 when @组合编号 then @sta+','
else case 已用票号 when '' then '' else 已用票号+',' end end+票号,
@nma=case 组合编号 when @组合编号 then @nma+1 else 已用票数+1 end,
@stb=replace(
case 组合编号 when @组合编号 then @stb else 结余票号 end,
','+票号+',',','),
@nmb=case 组合编号 when @组合编号 then @nmb-1 else 结余票数-1 end,
@组合编号=组合编号,
已用票号=@sta,已用票数=@nma,
结余票号=@stb,结余票数=@nmb

update a set 已用票号=b.已用票号
,已用票数=b.已用票数
,结余票号=case b.结余票数 when 0 then ''
else substring(b.结余票号,2,len(b.结余票号)-2) end
,结余票数=b.结余票数
from table1 a,#i b,(
select 组合编号,已用票数=max(已用票数)
from #i
group by 组合编号
)c where a.组合编号=b.组合编号
and c.组合编号=b.组合编号
and c.已用票数=b.已用票数
end
go

--插入数据测试
insert table2 select 'A-0000001-0000005','A','0000001'
union all select 'A-0000001-0000005','A','0000003'
union all select 'B-0000006-0000010','B','0000006'
union all select 'B-0000006-0000010','B','0000008'

--删除测试
delete from table2 where 票号='0000008'

--更新测试
update table2 set 票号='0000008' where 票号='0000006'
go

--显示处理结果
select * from table1
go

--删除测试
drop table table1,table2

/*--测试结果(自己看)--*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: