February 5, 2013 at 9:56 am
I would like to create a trigger that only gets executed when a specific field in a table is updated or a new record with a non null value inserted into that field. Is that something that can be done using triggers? Please advice.
Thanks
February 5, 2013 at 10:36 am
sqlstar2011 (2/5/2013)
I would like to create a trigger that only gets executed when a specific field in a table is updated or a new record with a non null value inserted into that field. Is that something that can be done using triggers? Please advice.Thanks
Triggers get executed when the relevant action is performed - INSERT, UPDATE or DELETE.
Inside a trigger you can see if the specific field is changed, or what new value is inserted, based on this your trigger can do whatever you want it to do.
February 5, 2013 at 11:27 am
CREATE TRIGGER Triggername
ON TableName AFTER INSERT
IF EXISTS (SELECT 1 FROM Inserted WHERE Column IS NOT NULL)
-- Inserted view only exists inside of triggers.
BEGIN
--- Write code here
END
CREATE TRIGGER Triggername
ON TableName AFTER UPDATE
IF UPDATE(ColumnName) -- UPDATE function only exists inside of triggers
BEGIN
-- Write code here
END
Kenneth FisherI was once offered a wizards hat but it got in the way of my dunce cap.--------------------------------------------------------------------------------For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/[/url]Link to my Blog Post --> www.SQLStudies.com[/url]
February 5, 2013 at 12:22 pm
Kenneth.Fisher (2/5/2013)
CREATE TRIGGER Triggername
ON TableName AFTER UPDATE
IF UPDATE(ColumnName) -- UPDATE function only exists inside of triggers
BEGIN
-- Write code here
END
This method will work but often people get confused. They think this will only happen if the value changes. The code inside that block will fire even if the value is updated to the current value. You would need to evaluate the inserted and deleted tables to determine if the value for a given column was changed.
In other words if you executed the code below, the IF UPDATE(MyColumn) would evaluate to true and the code inside would execute.
update MyTable
set MyColumn = MyColumn
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply