Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CHECK on a VIEW in SQL Server

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?

like image 555
EngineerSpock Avatar asked Nov 28 '25 03:11

EngineerSpock


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

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!