May 6, 2011 at 8:21 am
Hi All,
I met a scenario "When a coloumn of a table A is updated trigger should be performed like"AFTER UPDATE" and i need to insert values into table B".
The requirement is only when coloumn X is updated my trigger should be performed but my trigger is executing when any of the coloumns are updated.
Can anyone tell me how to write trigger for this scenario.
Thanks in Advance....
May 6, 2011 at 8:30 am
hemanth T (5/6/2011)
Hi All,I met a scenario "When a coloumn of a table A is updated trigger should be performed like"AFTER UPDATE" and i need to insert values into table B".
The requirement is only when coloumn X is updated my trigger should be performed but my trigger is executing when any of the coloumns are updated.
Can anyone tell me how to write trigger for this scenario.
Thanks in Advance....
INSERT INTO TABLE B( [Column List])
SELECT [Column List]
FROM INSERTED
INNER JOIN DELETED ON INSERTED.[PrimaryKey] = DELETED.[PrimaryKey]
WHERE INSERTED.[X] <> DELETED.[X]
Lowell
May 6, 2011 at 1:39 pm
You can also use the UPDATE function in the trigger. Just be aware that if you include the column in an update statement this function will consider that column updated even if the value does not change so you'd still want to use Lowell's code if you only want changes to the column.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
May 7, 2011 at 4:45 am
Hi Lowell,
I tried your code for my trigger.It is working fine.
Thank you for your valuable suggestion.
Hemanth
....................................................................................................................................................
May 7, 2011 at 4:53 am
Hi Jack,
I followed Lowell's code and its working fine.Just as you said UPDATE function may result in unnecessary INSERT's.
Thank you for extending my area of solution.
Hemanth
..........................................................................................................................................................
May 7, 2011 at 6:04 am
hemanth T (5/7/2011)
Hi Lowell,I tried your code for my trigger.It is working fine.
Thank you for your valuable suggestion.
Hemanth
....................................................................................................................................................
Glad it helped you build your trigger!
Understanding the relationship between the INSERTED and DELETED tables is all it takes to make a really good trigger.
way to go!
Lowell
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply