CHECK on a VIEW in SQL Server

Viewed 89

In PostgreSQL we have a 'WITH LOCAL CHECK OPTION' statement to enforce the check implemented in the WHERE clause.

For example,

CREATE VIEW HeavyOrders AS
SELECT *
FROM Orders
WHERE Weight > 100
WITH LOCAL CHECK OPTION;

now if we try to insert a raw where Weight < 100 we will get an error:

INSERT INTO HeavyOrders(x, y, z, Weight)
VALUES('a', 'b', 'c', 80);

Is there a way to enforce such a check on a VIEW?

1 Answers

Views can be created in SQL Server WITH CHECK OPTION. WITH CHECK OPTION will make sure that all INSERT and UPDATE statements executed against the view meet the restrictions in the WHERE clause, and that the modified data in the view remains visible after INSERT and UPDATE statements.

 CREATE VIEW HeavyOrders AS
    SELECT *
    FROM Orders
    WHERE Weight > 100
    WITH CHECK OPTION
Related