SQL-Server Triggers  «Prev  Next»

Modify a SQL Server 2025 Trigger

Objective: Alter an existing DELETE-only trigger so that it rejects both updates and deletions of existing sales rows.

Continue from the creating-trigger exercise

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.

Instructions

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:

  1. Use ALTER TRIGGER dbo.trgDelete ON dbo.Sales. Retain the existing name for continuity with the preceding exercise.
  2. Specify AFTER UPDATE, DELETE. Include both events in the replacement definition; INSERT must not activate this trigger.
  3. Retain SET NOCOUNT ON and explicitly set XACT_ABORT ON.
  4. Return without an error when 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.

Expected behavior

Cases to check after altering the trigger
OperationExpected outcome
UPDATE or DELETE one or more existing salesError 51042; the changes cannot be committed.
UPDATE an existing sale to its current valuesError 51042; this rule rejects matching UPDATE statements even when values are unchanged.
UPDATE or DELETE where SalesID = -1No trigger error because no row matches.
INSERT a new saleThis 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.

Self-assessment: 10 points

The website displays your response beside a model answer. It does not execute your SQL or assign an automatic grade.

RequirementPoints
Alters the existing dbo.trgDelete on dbo.Sales2
Specifies AFTER UPDATE, DELETE with a complete body and separate batch3
Checks deleted as a set and permits zero-row operations2
Includes NOCOUNT, XACT_ABORT, and the specified THROW error3

Submit your answer

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.