datetime を含むテーブルがあり、挿入された値に対して +-30 分の期間に datetime を含むエントリがあるかどうかを確認したいと考えています。だから私はこの制約を書きます:
USE [Test]
GO
/****** Object: UserDefinedFunction [dbo].[CanInsertReception] Script Date: 23.09.2015 12:19:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[CanInsertReception] (@receptionBegin datetime)
RETURNS bit
AS
BEGIN
DECLARE @result bit;
IF EXISTS(SELECT * FROM Main
where DATEDIFF(MINUTE, ReceptionBegin, @receptionBegin) <= 30 or DATEDIFF(MINUTE, @receptionBegin, ReceptionBegin) <= 30)
SET @result = 0
ELSE
SET @result = 1
return @result;
END;
GO;
ALTER TABLE Main
ADD CONSTRAINT CheckIfCanInsertReception
CHECK (dbo.CanInsertReception(ReceptionBegin) = 1);
しかし、データを挿入しようとすると、このチェックでは挿入が許可されませんが、このスクリプトを実行すると、同じことが行われます:
DECLARE @receptionBegin datetime = '2015-01-01 09:00:00'
DECLARE @result bit;
IF EXISTS(SELECT * FROM Main
where DATEDIFF(MINUTE, ReceptionBegin, @receptionBegin) <= 30 or DATEDIFF(MINUTE, @receptionBegin, ReceptionBegin) <= 30)
SET @result = 0
ELSE
SET @result = 1
SELECT @result
期待される出力が得られます1
ここで何が間違っていますか?