if exists (select 1 from tb  where datediff(dd,日期1,日期2)>30)
 print('超出范围')

解决方案 »

  1.   

    你能给我写个完整的吗?我想叫这超出的天数作为字段days中的值???谢谢了~~~~~~
      

  2.   

    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 行
    超出时间范围!*/