Hi
Well you can't specify it programmatically to only 'fire' if a certain field is modified but you can do this.
Note: I say 'modified' but you can pretty much do the same thing for inserted and deleted.
First off each trigger runs in an implicit transaction, when a trigger performs it's work it uses it's own temp tables to manage the data and then commits or rollback the work.
These two temp tables are called "inserted" and "deleted".
Now, something else you want to know is that when a trigger inserts, it puts the new info into the "inserted" tables, makes sure thre are no errors and then commits, effectively taking the info from inserted and actuallyl inserting it into the table.
When you delete info, it uses the temp table called "Deleted" and it does the same. Takes the info being deleted and put it into "Deleted", makes sure there are no errors and commmits, effectively purging the "Deleted" table.
What does an update do? Uses both actually, it takes the information being replace and moves/copies it to the deleted tables, it then take the 'new' information in the 'update' commant and puts it into the "inserted" table. Then if no errors, it purges the "deleted" table and takes what's in the "inserted" Table and puts it into the actualy table being updated.
How does this help you?
Easy.. watch...
Let's assume we have a table called Clients and it looks like this.
CREATE TABLE Clients (
clientID numeric(12,0) identity not null primary key,
name varchar(50) not null,
contact varchar(50) not null,
someFieldThatCanNeverChange varchar(1) not null
)
When you have an update trigger on this table, you can check the "deleted" (before) data and the "inserted" (after) data, if they are different you can either rollback the change (effectively ending the trigger and ignoring the update).
Ex.
CREATE TRIGGER upd_trigger_Clients
ON Clients
FOR UPDATE
AS
BEGIN
DECLARE @check numeric(12,0)
SELECT @check = -1
SELECT @check = D.clientID
FROM inserted I, deleted D
WHERE
LTRIM( RTRIM( I.someFieldThatCanNeverChange ) )
<>
LTRIM( RTRIM( D.someFieldThatCanNeverChange ) )
IF( @check <> -1 )
BEGIN
-- Looks like the information HAS changed, so let's NOT do the update
ROLLBACK
END
ELSE
BEGIN
-- DO whatever else you want to happen when trigger fired
COMMIT
END
END
Hope this helps.
-=-=-=-=-=-=-=-=-
For ease of reading, any posted CODE should be wrapped by [ignore][COLOR] and
[/ignore] tags.
Ex: