I am trying to replace simple CHECK constraint with an embedded function within a CHECK constraint however it doesn't seem to restrict the data I can enter. The constraint is to prevent product amount of less than 0.25 and more than 5,000. The original check worked fine, however the function doesn't seem to do anything at all.
The original constraint:
ALTER TABLE Prices
ADD CONSTRAINT CheckPrices CHECK ((Amount > 0.25) AND (Amount <= 5000.00))
The function:
ALTER FUNCTION dbo.CheckProductPrices
(
@productSKU int,
@priceDate smalldatetime
)
RETURNS bit
AS
BEGIN
DECLARE @retVal bit = 0
SELECT @retVal = CASE WHEN Amount > 0.25 AND Amount <= 5000.00 THEN 1 ELSE 0 END
FROM Prices
WHERE productSKU = @productSKU
AND priceDate = @priceDate
RETURN @retVal
END
The new CHECK constraint:
ALTER TABLE Prices
ADD CONSTRAINT CheckPrices CHECK (dbo.CheckProductPrices([productItem], [priceValidDate]) = 1)
I don't understand why the new constraint isn't stopping invalid prices the way the original constraint did.