日期:2014-05-16 浏览次数:20717 次
if (object_id('test') is not null)
drop table test
create table test
(
ID int identity(1,1) primary key,
BDate datetime null,
EDate datetime null
)
if (object_id('tgr_classes_insert', 'tr') is not null)
drop trigger tgr_classes_insert
go
create trigger tgr_classes_insert
on test
after insert --插入触发
as
begin
declare @ID int
declare @beginDate datetime
declare @endDate datetime
select @ID=ID, @beginDate=BDate,@endDate=EDate from inserted --保存了引发新增触发器的新增数据,只能在触发器中访问
if(@beginDate>=@endDate)
begin
delete from test where ID=@ID
end
end
go
--插入数据
insert into test values('2014-04-21','2014-04-1');
--查询数据
select * from test;
use tempdb
go
Create table T(BDate datetime,EDate datetime )
go
create trigger tr_cT on T
for insert,update
as
if exists(select 1 from inserted where BDate!<EDate)
begin
print N'BDate!<EDate'
rollback tran
end
go
insert t select '2014-01-01','2014-02-01' --Success
go
insert t select '2014-02-01','2014-02-01' --Error
/*
BDate!<EDate
消息 3609,级别 16,状态 1,第 1 行
事务在触发器中结束。批处理已中止。
*/
go
select * from t
go
drop table t