devxlogo

The WITH CHECK OPTION option with views

The WITH CHECK OPTION option with views

SQL Server’s views are updateable, but it is possible that when you insert a new record or update an existing record, the record added or modified doesn’t logical belog to the view any longer. For example, consider the following view:

CREATE VIEW authors_CA AS    (         SELECT * FROM Authors WHERE state=’CA’    )

If you now issue the following command:

UPDATE authors_CA SET state=’NJ’

then all the records visible in the view are updated with a differente State value, and therefore would disappear from the view itself. This is perfectly legal, but can create subtle programming errors. You can avoid this problem by adding the WITH CHECK OPTION predicate when you create the view:

CREATE VIEW authors_CA AS    (         SELECT * FROM Authors WHERE state=’CA’    )    WITH CHECK OPTION

Now any insert or update operation that makes a record disappear from the view raises a trappable runtime error.

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist