recid | EffectiveFrom | EffectiveTo
------------------------------------
1 | 2012-11-25 | 2012-11-30
2 | 2012-11-26 | null
我需要一个触发器来检查新日期是否介于effectivefrom
和之间effectiveTo
并停止插入..
recid | EffectiveFrom | EffectiveTo
------------------------------------
1 | 2012-11-25 | 2012-11-30
2 | 2012-11-26 | null
我需要一个触发器来检查新日期是否介于effectivefrom
和之间effectiveTo
并停止插入..
尝试这个 :
USE {yourDB};
CREATE TRIGGER insertRecord ON {tblName}
INSTEAD OF INSERT
AS
BEGIN
DECLARE @EffectiveFrom DATE;
DECLARE @EffectiveTo DATE;
DECLARE @newDate DATE;
SET @newDate = GETDATE(); -- DATE WHICH YOU WANT TO CHEK
SELECT @EffectiveFrom = EffectiveFrom FROM INSERTED;
SELECT @EffectiveTo = EffectiveTo FROM INSERTED;
IF (@newDate > @EffectiveFrom AND @newDate < @EffectiveTo)
BEGIN
INSERT INTO [{yourDB}].[dbo].[{tblName}]
SELECT * FROM INSERTED;
PRINT 'DATA SUCCESSFULLY ADDED.'
END
ELSE PRINT 'ERROR : DATE IS NOT IN BETWEEN EffectiveFrom and EffectiveTo DATE'
END
INSERT INTO [{yourDB}].[dbo].[{tblName}] VALUES(CAST('24-NOV-2012' AS DATE),CAST('30-NOV-2012' AS DATE))
消息 --result 错误:日期不在 EffectiveFrom 和 EffectiveTo DATE 之间
INSERT INTO [{yourDB}].[dbo].[{tblName}] VALUES(CAST('24-NOV-2012' AS DATE),CAST('02-DEC-2012' AS DATE))
消息——结果(1 行受影响)数据已成功添加。