Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2005
»
T-SQL (SS2K5)
»
If Update() in a trigger
If Update() in a trigger
Rate Topic
Display Mode
Topic Options
Author
Message
dean.giberson 64357
dean.giberson 64357
Posted Monday, September 17, 2012 2:50 PM
Grasshopper
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 8:59 AM
Points: 12,
Visits: 27
I have a question that I thought I knew the answer to but I am now not sure. When in a trigger, suppose there are 10 rows in the inserted table. 3 rows have one column(call it A) updated and 7 rows have another column (call it B) updated. If code is entered in the trigger that states If Updated(A) begin ... end will the actions between the begin and end be executed on all 10 rows or only the 3 with the column A updated?
Post #1360457
GilaMonster
GilaMonster
Posted Monday, September 17, 2012 3:06 PM
SSC-Dedicated
Group: General Forum Members
Last Login: Today @ 5:49 PM
Points: 37,671,
Visits: 29,925
A trigger fires once only for an operation. All the rows affected by the insert/update/delete will be in the inserted and/or deleted tables.
Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild
: Discussions on DB performance with occasional diversions into recoverability
We walk in the dark places no others will enter
We stand on the bridge and no one may pass
Post #1360466
Jeff Moden
Jeff Moden
Posted Monday, September 17, 2012 6:32 PM
SSC-Dedicated
Group: General Forum Members
Last Login: Today @ 5:33 PM
Points: 32,902,
Visits: 26,783
dean.giberson 64357 (9/17/2012)
I have a question that I thought I knew the answer to but I am now not sure. When in a trigger, suppose there are 10 rows in the inserted table. 3 rows have one column(call it A) updated and 7 rows have another column (call it B) updated. If code is entered in the trigger that states If Updated(A) begin ... end will the actions between the begin and end be executed on all 10 rows or only the 3 with the column A updated?
In a single insert or update of all 10 rows for the above, all 10 rows will respond to the IF UPDATED(A) because IF UPDATED() simply checks to see if anything in the whole column for the whole statement was affected.
--Jeff Moden
"
RBAR
is pronounced "ree-bar" and is a "Modenism" for "
R
ow-
B
y-
A
gonizing-
R
ow".
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1360512
dean.giberson 64357
dean.giberson 64357
Posted Tuesday, September 18, 2012 5:37 AM
Grasshopper
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 8:59 AM
Points: 12,
Visits: 27
Thanks for the reply. So I am clear on the answer, whatever the processing is between the begin and end will be executed because the system is only checking if column A has been updated for any of the rows in the inserted table so even if only one of the 10 rows had column A updated all 10 rows would still be processed. Am I correct?
Post #1360702
GilaMonster
GilaMonster
Posted Tuesday, September 18, 2012 5:54 AM
SSC-Dedicated
Group: General Forum Members
Last Login: Today @ 5:49 PM
Points: 37,671,
Visits: 29,925
Not quite...
It's checking whether ColumnA was one of the columns specified in the SET clause of the update statement.
UPDATE Tbl SET ColumnA = ColumnA
If you had an update trigger on Tbl checking for UPDATE(ColumnA), it would be true for that update statement, even though no rows have the value of ColumnA changed.
Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild
: Discussions on DB performance with occasional diversions into recoverability
We walk in the dark places no others will enter
We stand on the bridge and no one may pass
Post #1360713
dean.giberson 64357
dean.giberson 64357
Posted Tuesday, September 18, 2012 5:58 AM
Grasshopper
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 8:59 AM
Points: 12,
Visits: 27
Gotcha, not CHANGED just UPDATED.
Post #1360720
GilaMonster
GilaMonster
Posted Tuesday, September 18, 2012 6:04 AM
SSC-Dedicated
Group: General Forum Members
Last Login: Today @ 5:49 PM
Points: 37,671,
Visits: 29,925
Yup. You want changed, you need to compare the columns in the inserted and deleted tables.
Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild
: Discussions on DB performance with occasional diversions into recoverability
We walk in the dark places no others will enter
We stand on the bridge and no one may pass
Post #1360723
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.