日期:2014-05-18 浏览次数:20749 次
if object_id('tb') is not null
drop table tb
go
create table tb(cid int,c_no varchar(8))
insert tb select 1,'20090501'
insert tb select 2,'20090502'
insert tb select 3,'20090503'
insert tb select 4,'20081231'
select * from tb
--创建触发器
if object_id('tri_update') is not null
drop trigger tri_update
go
create trigger tri_update on tb
for delete
as
begin
declare @cid int
select @cid=cid from deleted
update tb
set c_no=convert(char(8),dateadd(day,1,c_no),112)
where cid>@cid
end
go
--删除cid=2
delete from tb
where cid=2
--查看数据
select * from tb
--删除触发器和表
drop trigger tri_update
drop table tb
/*
(1 行受影响)
(1 行受影响)
(1 行受影响)
(1 行受影响)
cid c_no
----------- --------
1 20090501
2 20090502
3 20090503
4 20081231
(4 行受影响)
(2 行受影响)
(1 行受影响)
cid c_no
----------- --------
1 20090501
3 20090504
4 20090101
(3 行受影响)
*/
------解决方案--------------------
create table test(cid int ,c_no datetime) insert test select 1, '20090501' union select 2, '20090502' union select 3, '20090503' create trigger t_test on test after delete as update test set c_no = dateadd(day,-1,c_no) from test delete from test where c_no='20090501' select * from test --- 2 2009-05-01 00:00:00.000 3 2009-05-02 00:00:00.000
------解决方案--------------------
看来这个字段的值不必存于数据库,因为只是给人看的
不过用户可能需要以此字段来查找,所以又需要能被检索。
方案:
1、存储与显示:数据库里存 年月部分,配合 row_number()得序号字段,组合成该值以供查看。
2、检索:对于用户输入 20090512 之类的值,解析为年月以及序号,用 1的办法 取出带序号的记录集,
即可匹配。
------解决方案--------------------
这个在财务软件中有用到,就是记账凭证号,当作废一张凭证后就要整理断号以保证号码的连续性,不过它有一个前提就是每个月必须结完账后才能做下一个月的,这样整理起来也就是一个月的数据量,对数据库压力不大,如果整个库每改一次都整理一次,有个几万十万的数据的时候,哭都哭不出来了!
------解决方案--------------------