70-466 · Question #121
You have an application that uses a view to access data from multiple tables. You need to ensure that you can insert rows into the underlying tables by using the view. What should you do?
The correct answer is C. Create an INSTEAD OF trigger on the view. Views spanning multiple tables cannot natively accept INSERT statements. An INSTEAD OF trigger intercepts the DML operation and allows custom logic to route inserts to the correct underlying tables.
Question
Options
- ADefine the view by using the SCHEMABINDING option.
- BDefine the view by using the CHECK option.
- CCreate an INSTEAD OF trigger on the view.
- DMaterialize the view.
How the community answered
(41 responses)- A2% (1)
- B15% (6)
- C76% (31)
- D7% (3)
Why each option
Views spanning multiple tables cannot natively accept INSERT statements. An INSTEAD OF trigger intercepts the DML operation and allows custom logic to route inserts to the correct underlying tables.
SCHEMABINDING binds the view to its underlying objects to prevent schema changes but does not enable INSERT operations through a multi-table view.
WITH CHECK OPTION ensures rows inserted or updated through the view still satisfy the view's WHERE predicate, but it does not resolve the restriction that prevents inserts on views joining multiple tables.
An INSTEAD OF trigger fires in place of the actual DML statement on the view, letting you write custom logic to decompose a single INSERT into separate inserts against each underlying base table. This is the standard SQL Server mechanism for enabling DML through views that would otherwise be non-updatable due to joining multiple tables. Without it, any direct INSERT through a multi-table view would raise an error.
Materializing a view via an index improves read query performance but does not enable inserts through a view that spans multiple base tables.
Concept tested: INSTEAD OF trigger enabling DML through views
Source: https://learn.microsoft.com/en-us/sql/relational-databases/triggers/dml-triggers
Topics
Community Discussion
No community discussion yet for this question.