Objective: Alter an existing DELETE-only trigger so that it rejects both updates and deletions of existing sales rows.
Use the same dedicated practice database as the creating-trigger exercise. Its setup creates dbo.Sales with columns SalesID int and SaleTotal decimal(10, 2), and two rows: (1, 50.00) and (2, 75.00). Its model answer creates dbo.trgDelete, which rejects DELETE statements affecting existing rows.
Complete that setup and create the original trigger before starting here. Keep the existing table and trigger. You need ALTER permission on the table to modify its DML trigger. Finish any earlier test transaction before continuing.
The revised rule is: existing sales rows must not be updated or deleted; new sales may still be inserted. Write the complete replacement trigger definition:
ALTER TRIGGER dbo.trgDelete ON dbo.Sales. Retain the existing name for continuity with the preceding exercise.AFTER UPDATE, DELETE. Include both events in the replacement definition; INSERT must not activate this trigger.SET NOCOUNT ON and explicitly set XACT_ABORT ON.deleted is empty. Otherwise, use THROW with error number 51042, message Existing rows in dbo.Sales cannot be updated or deleted., and state 1.Handle the complete affected set, including statements that match multiple rows. Put the ALTER TRIGGER definition in its own batch. Submit the full definition rather than only the lines you changed.
| Operation | Expected outcome |
|---|---|
| UPDATE or DELETE one or more existing sales | Error 51042; the changes cannot be committed. |
| UPDATE an existing sale to its current values | Error 51042; this rule rejects matching UPDATE statements even when values are unchanged. |
| UPDATE or DELETE where SalesID = -1 | No trigger error because no row matches. |
| INSERT a new sale | This trigger does not run; the table's constraints still apply. |
Hint: For both UPDATE and DELETE, deleted holds the affected rows' previous values. That makes it useful for checking whether either operation matched any rows. If a caller catches the error, it must roll back any remaining failed transaction before continuing. The result page provides separate tests for both events.
The website displays your response beside a model answer. It does not execute your SQL or assign an automatic grade.
| Requirement | Points |
|---|---|
| Alters the existing dbo.trgDelete on dbo.Sales | 2 |
| Specifies AFTER UPDATE, DELETE with a complete body and separate batch | 3 |
| Checks deleted as a set and permits zero-row operations | 2 |
| Includes NOCOUNT, XACT_ABORT, and the specified THROW error | 3 |
Paste your complete ALTER TRIGGER statement below. Add a short explanation of why the same zero-row check works for both UPDATE and DELETE.
References: ALTER TRIGGER and the inserted and deleted tables.