检查约束不允许我添加数据

时间:2019-10-23 13:49:52

标签: sql-server tsql stored-functions check-constraints

我有一个用户定义的函数,该函数返回BIT。

  • 1-如果日期重叠
  • 0-如果日期不重叠

我已经测试了UDF,它似乎运行正常。

这是我添加的以下检查约束:

ALTER TABLE [Gizmo] WITH CHECK ADD CONSTRAINT [CK_DateOverlap]
CHECK [dbo].[MyUserFunction]([GizmoName],[GizmoState],[BeginDate],[EndDate]) = 0;

该表中没有任何数据。当我插入新记录时,出现错误

The INSERT statement conflicted with the CHECK constraint

我手动调用UDF进行测试,它返回0。

如果UDF返回0,我希望约束添加记录。如果返回1,我不想添加记录。

我想念什么?

编辑-UDF

ALTER FUNCTION [dbo].[MyUserFunction]
(
     @GizmoName AS VARCHAR(max),
     @GizmoState AS VARCHAR(max),
     @BeginDate AS DATETIME,
     @EndDate AS DATETIME

)

RETURNS BIT
AS
BEGIN
       DECLARE @Result BIT
       IF EXISTS(
                    SELECT *
                    FROM Gizmos
                    WHERE (@GizmoName = Name AND @GizmoState = [State])
                    AND (@EndDate >= EffectiveFrom AND EffectiveTo >= @BeginDate)
                    )
                    BEGIN
                        SET @Result = 1;
                    END
                    ELSE
                    BEGIN
                           SET @Result = 0;   
                    END
       RETURN @Result
END

插入

INSERT INTO Gizmos VALUES('XXX', 'CA', '1/1/2019', '12/31/2019');

1 个答案:

答案 0 :(得分:1)

该行已插入到表中,并且约束应用于表。因此,数据已经可用,函数将返回1并回滚事务。因此我们需要忽略第一行,然后按预期运行。

CREATE OR ALTER FUNCTION [dbo].[MyUserFunction]
(
     @GizmoName AS VARCHAR(max),
     @GizmoState AS VARCHAR(max),
     @BeginDate AS DATETIME,
     @EndDate AS DATETIME

)

RETURNS BIT
AS
BEGIN
       DECLARE @Result BIT

       IF (SELECT COUNT(Name)
                    FROM Gizmos
                    WHERE (@GizmoName = Name AND @GizmoState = [State])
                    AND (@EndDate >= EffectiveFrom AND EffectiveTo >= @BeginDate)
                    ) > 1
                    BEGIN
                        SET @Result = 1;
                    END
                    ELSE
                    BEGIN
                           SET @Result = 0;   
                    END

                    RETURN @Result

END
GO