SQL Server check constraint failing on correct input

走远了吗. 提交于 2019-12-01 22:29:42

When the CHECK constraint fires, the row is already in the table. Therefore, the function is called, and since there is a row returned by the query, the function returns 1, not 0. Try this. Drop the constraint, insert your row successfully, and then run this query:

SELECT OLIID, PID, dbo.fnObjProd([PID]) FROM dbo.ObjInstanceProd;

It should return 1 for every value of PID. Try to add the constraint now. It will fail for the same reason.

Have you considered using a trigger for this? If you use a check constraint, this will turn any multi-row insert or update into a cursor behind the scenes. This can absolutely kill performance and concurrency depending on how you touch your tables. Here is a simple INSTEAD OF INSERT trigger to prevent bad values going in with a single operation, even for a multi-row insert:

CREATE TRIGGER dbo.trObjProd 
ON dbo.ObjInstanceProd
INSTEAD OF INSERT AS
BEGIN
    SET NOCOUNT ON;

    IF NOT EXISTS
    (
      SELECT 1 FROM inserted
      WHERE EXISTS 
      (
        SELECT 1
            FROM dbo.ObjInstanceProd AS olip
            INNER JOIN dbo.ObjInstance AS oli 
            ON olip.OLIID = oli.OLIID
            INNER JOIN dbo.ObjLevel AS ol
            ON ol.OLID = oli.OLID
        WHERE 
            ol.Name in ('Toyota','Lexus')
            AND olip.PID = inserted.PID
      )
  )
  BEGIN
    INSERT ObjInstanceProd(OLIID, PID)
        SELECT OLIID, PID FROM inserted;
  END
  ELSE
  BEGIN
    RAISERROR('At least one value was not good.', 11, 1); 
    SELECT OLIID, PID FROM inserted;
  END
END
GO

If you're going to stick with a function, this is a much more efficient approach, however you need to define a way to determine that the current row being inserted is excluded from the check - I couldn't determine how to do that because there are no constraints on dbo.ObjInstanceProd. Is OLIID, PID unique?

ALTER FUNCTION [dbo].[fnObjProd]
(
    @Pid INT
) 
RETURNS BIT
WITH EXECUTE AS CALLER 
AS
BEGIN
    RETURN 
    (
        SELECT CASE WHEN EXISTS 
        (
            SELECT 1 
                FROM dbo.ObjInstanceProd AS olip
                INNER JOIN dbo.ObjInstance AS oli 
                ON olip.OLIID = oli.OLIID
                INNER JOIN dbo.ObjLevel AS ol
                ON ol.OLID = oli.OLID
            WHERE 
                ol.Name in ('Toyota','Lexus')
                AND olip.PID = @Pid
        ) THEN 1 ELSE 0 END
    );
END
GO
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!