From the course: Complete Guide to Advanced SQL Server

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Create a database trigger

Create a database trigger

Database triggers are a special kind of stored procedure. Instead of running when they're manually executed, triggers automatically fire when a specific activity occurs on the server. You can attach triggers to data manipulation events such as insert, update, and delete commands, or you can have them fire when the database processes a data definition command, such as create or alter table. Let's see how triggers work by setting up a system that will automatically log changes to the Colors table in a new Audit table. First, I'm going to create a table for the audit. I'll place it inside of the warehouse schema and I'll call the table ColorAudit. It's going to have three columns. The first column AuditID will be an integer value. It'll be the identity column. And it'll be the primary key for the table. Then we'll add a column for the ColorName, as well as a datetime column called TimeAdded. So let's go ahead and create that ColorAudit table. And now we can turn our attention to creating…

Contents