Summary of "After update trigger Part 44"

Video Summary: After Update Trigger - Part 44 (SQL Server)

Storyline / Context

This video is part 44 of a SQL Server tutorial series by Preim Technologies, focusing on after update triggers. Earlier videos (part 42) covered after insert and after delete triggers, as well as the types of triggers in SQL Server: DML, DDL, and logon triggers. This session explains how after triggers work, specifically after update triggers, and demonstrates their practical use.


Key Concepts Discussed


Gameplay Highlights / Demonstration


Step-by-Step Guide to Creating an Audit Trigger for Updates

Goal: Audit changes made to employee data and log the changes into the TBL_employee_audit table.

Audit Table Structure: - Has an identity primary key. - Contains an audit_data column to store audit strings.

Trigger Logic:

  1. Create or alter an after update trigger on [TBL_employee](https://www.amazon.com/dp/1943872570?tag=dtdgstoreid08-20).
  2. Declare variables to hold old and new values for each column (Name, Salary, Gender, DepartmentID).
  3. Use a temporary table to hold rows from the inserted table.
  4. Use a loop to process each updated row individually (to handle multiple updates in one statement).
  5. For each row:
    • Retrieve new values from the temporary table.
    • Retrieve old values from the deleted table using the same ID.
    • Compare old and new values for each column.
    • Build an audit string describing the changes, e.g., "Employee with ID=2 changed name from My to MyKey".
    • Insert the audit string into the audit table.
    • Delete the processed row from the temporary table to avoid infinite loops.
  6. Commit the trigger.

Key Tips / Strategies


Example Output

After updating employee with ID=4, the audit table logs:

“Employee with ID=4 changed name from Todd to Todds gender from male to female salary from 4800 to 2000”


Resources Mentioned


Featured Gamer / Source


End of Summary

Category ?

Gaming

Share this summary

Featured Products

Video