自动生成剩余编号字符串

<iframe align="top" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog01.html" frameborder="0" width="728" scrolling="no" height="90"></iframe>

原帖地址:

http://community.csdn.net/Expert/topic/3220/3220744.xml?temp=.6029474

问题描述:

insert 主表 select 'Q',0000001,0000100,100,'0000001-0000100'?
union all select 'M',0000001,0000200,200,'0000001-0000200'

insert 子表 select 'Q',0000011,0000030,20
union all select 'Q',0000032,0000050,19
union all select 'Q',0000061,0000080,20
union all select 'M',0000001,0000100,100

如何用触发器在子表中插入每条记录时,更新主表的balance字段呢?

如上想得出的结果是:
id name strat end amount balance
1 Q 0000001 0000100 41 0000001-0000010,0000031-0000031,0000051-0000060,0000081-0000100
2 M 0000001 0000200 100 0000101-0000200

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

--测试数据
CREATE TABLE [dbo].[主表] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (50) NULL ,
[strat] [varchar] (7) NULL ,
[end] [varchar] (7) NULL ,
[amount] [int] NULL ,
[balance] [varchar] (200) NULL
)

CREATE TABLE [dbo].[子表] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (50) NULL ,
[strat] [varchar] (7) NULL ,
[end] [varchar] (7) NULL ,
[amount] [int] NULL
)

insert 主表 select 'Q','0000001','0000100',100,'0000001-0000100'
union all select 'M','0000001','0000200',200,'0000001-0000200'
go

--处理的触发器
create trigger tr_process on 子表
for insert,update,delete
as
select *,[balance]=cast(null as [varchar] (200))
into #t
from 子表 a
where exists(select 1 from inserted where name=a.name)
or exists(select 1 from deleted where name=a.name)
order by name,[strat]

declare @name varchar(10),@end int,@re varchar(200)
update #t set @re=case @name when name
then case when @end+110000001+@end,7)+'-'+right(9999999+[strat],7)
else @re+'' end else '' end
,@end=[end],@name=name,[balance]=@re

update a set [balance]
=case when a.[strat]<b. then a. else end> +case when a.[strat]<b. then b.> else case when b.[balance]'' then stuff(b.[balance],1,1,'') else '' end end <br> +case when b.[end]<a.> then case when a.[strat]<b. or b.>''<br> then ',' else '' end+right(10000001+b.[end],7)+'-'+a.[end] <br> else '' end<br>,[amount]=cast(a.[end] as int)-a.[strat]-b.[amount]+1<br>from 主表 a,(<br>select name,[strat]=min([strat]),[end]=max([end]),[balance]=max([balance]),[amount]=sum([amount])<br>from #t<br>group by name<br>)b where a.name=b.name</b.></a.></b.></b.>

--处理在子表中被全部删除的数据
if exists(select 1 from deleted a where not exists(select 1 from 子表 where name=a.name))
update a set amount=cast(a.[end] as int)-a.[strat]+1
,[balance]=a.[strat]+'-'+a.[end]
from 主表 a,(
select distinct name from deleted a
where not exists(select 1 from 子表 where name=a.name)
)b where a.name=b.name
go

--插入子表数据
insert 子表 select 'Q','0000011','0000030',20
union all select 'Q','0000032','0000050',19
union all select 'Q','0000061','0000080',20
union all select 'M','0000051','0000100',50

--更新
update 子表 set name='M'
where ID=1

delete from 子表 where id in(1,3,4)
go

--显示处理结果
select * from 主表
select * from 子表
go

drop table 主表,子表

/*--测试结果

ID name strat end amount balance
--- ------ ------- ------- -------- -------------------------------
1 Q 0000001 0000100 81 0000001-0000031,0000051-0000100
2 M 0000001 0000200 200 0000001-0000200

(所影响的行数为 2 行)

ID name strat end amount
----------- ------ ------- ------- ----------
2 Q 0000032 0000050 19

(所影响的行数为 1 行)
--*/



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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值