Creating an SQL Trigger (SQL Server 7/2000)
So what exactly is a trigger? Well, it's a piece of sql that is activitied when a certain event happens. A common usage is when a new record is added to a database, this triggers an action - for example, a new order triggers an email to the supplier. It can be very useful.
To relative SQL newbies triggers and stored procedures can seem a bit daunted, but you'll quickly see there's really nothing to them.
To create a trigger, go to the Enterprise Manager, and your database. Select the table that causes the event to happen, right click > All Tasks > Manage Triggers and select new from the drop down box.
Here we're going insert a new record into the table directory_update whenever a record is inserted into the directory table.
What this particular code does is inserts the latest record id - max(directory_id) - from the directory, and the current date, into the table directory_update. It does this whenever a new record is inserted into the directory.
I used something similar to this to create 'last updated' flags within a directory structure - but that's another article. For now, I hope that's been a useful introduction for you into the world of Triggers.
|
|