日期:2014-05-17 浏览次数:20584 次
--创建表
if OBJECT_ID('TableA','u') is not null
drop table TableA
create table TableA
(
isTop bit,
endDate datetime
)
go
--触发器UpdateIsTop
--更新时修改isTop
if OBJECT_ID('UpdateIsTop','TR') is not null
drop trigger UpdateIsTop
go
create trigger MotifyIsTop
on TableA
for update
as
declare @FlagTime datetime
select @FlagTime = endDate from inserted
if(@FlagTime<GETDATE())
begin
update TableA set isTop=1 where endDate=@FlagTime
end
go
--触发器InsertRecord
--插入时修改isTop
if OBJECT_ID('InsertRecord','TR') is not null
drop trigger InsertRecord
go
create trigger InsertRecord
on TableA
for insert
as
declare @FlagTime datetime
select @FlagTime = endDate from inserted
if(@FlagTime<GETDATE())
begin
update TableA set isTop=1 where endDate=@FlagTime
end
go
--测试插入
insert into TableA
select 0,DATEADD(D,2, GETDATE())
--结果
--isTop endDate
--1 2012-12-06 13:28:51.283 //注意这条(isTop被值为1)
--测试修改
--修改前数据
--isTop endDate
--1 2012-12-06 13:28:51.283
--0 2012-12-10 13:30:34.540 //注意这条
update TableA set endDate='2012-09-20 20:33:33' where endDate>GETDATE()
--修改前数据
--isTop endDate
--1 2012-09-20 20:33:33.000
--1 2012-09-20 20:33:33.000 //注意这条(isTop被值为1)