Use test Go Set Nocount On If object_id('test') Is Not Null Drop Table test Go Create Table test ( ID int IDentity(1,1), date datetime , Constraint PK_test_ID Primary Key(ID Asc) ) Go Create Trigger tg_test_Insert On test For Insert As If Exists(Select 1 From Inserted As a Where Exists(select 1 From test Where Datediff(day,date,a.date)>30)) Begin Raiserror 50001 N'超出时间范围!' Rollback Tran Begin Tran End Go Insert Into test Select '20090501' Go Insert Into test Select '20090502' Go Insert Into test Select '20090701'/* 消息 50001,级别 16,状态 1,过程 tg_test_Insert,第 5 行 超出时间范围!*/
Go
Set Nocount On
If object_id('test') Is Not Null
Drop Table test
Go
Create Table test
(
ID int IDentity(1,1),
date datetime ,
Constraint PK_test_ID Primary Key(ID Asc)
)
Go
Create Trigger tg_test_Insert On test For Insert
As
If Exists(Select 1 From Inserted As a Where Exists(select 1 From test Where Datediff(day,date,a.date)>30))
Begin
Raiserror 50001 N'超出时间范围!'
Rollback Tran
Begin Tran
End
Go
Insert Into test Select '20090501'
Go
Insert Into test Select '20090502'
Go
Insert Into test Select '20090701'/*
消息 50001,级别 16,状态 1,过程 tg_test_Insert,第 5 行
超出时间范围!*/