Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Trigger for multiple columns - Insert / update


SQL Trigger for multiple columns - Insert / update

Author
Message
Minnu
Minnu
Old Hand
Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)

Group: General Forum Members
Points: 305 Visits: 950
hi Team,


I want to create a trigger, that that should fire when ever particular columns are updated/inserted.

am using below query.. is it correct way.

CREATE TRIGGER [TRG_TESTING]
ON TABLE_NAME
AFTER INSERT,UPDATE
AS
SET NOCOUNT ON
IF (UPDATE (Col1,Col2,Col3) OR
(INSERT (Col1,Col2,Col3)


DECLARE
@Var1 varchar(max),
@ID INT
---
---
--

.
anthony.green
anthony.green
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6091 Visits: 6069
Look at the COLUMNS_UPDATED() clause

http://msdn.microsoft.com/en-us/library/765fde44-1f95-4015-80a4-45388f18a42c



Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
When a question, really isn't a question - Jeff Smith
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


toddasd
toddasd
SSC-Addicted
SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)

Group: General Forum Members
Points: 480 Visits: 3792
UPDATE takes only one column. There is no similar 'INSERT' function; UPDATE checks for both.

Your line would change to

IF (UPDATE(Col1) OR UPDATE(Col2) OR UPDATE(Col3))

You could also use COLUMNS_UPDATED, but that may be confusing with the bit mask.

Edit: removed quote

______________________________________________________________________________
How I want a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14942 Visits: 38935
I think your trigger model needs to look more like this.
because triggers handle multiple rows in SQL server, you should never declare a variable in a trigger, because it makes you think of one row/one value, instead of the set.
there are exceptions of course, but it's a very good rule of thumb.


also note the UPDATE function doesn't tell you the VALUE changed on a column...only whether the column was included int eh column list for insert/update. so if it was updated to the exisitng value (and a lot of data layers will do that automatically) it's a false detection of a change.

CREATE TRIGGER [TRG_TESTING]
ON TABLE_NAME
AFTER INSERT,UPDATE
AS
SET NOCOUNT ON

INSERT INTO SomeTrackingTable(ColumnList)
SELECT ColumnList
FROM INSERTED
LEFT OUTER JOIN DELETED
ON INSERTED.SomePrimaryKey = DELETED.SomePrimaryKey
WHERE DELETED.SomePrimaryKey IS NULL --inserted only
OR (INSERTED.SpecificColumn <> DELETED.SpecificColumn) --this column changed...so we need to log it.



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

TheSQLGuru
TheSQLGuru
SSCertifiable
SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)

Group: General Forum Members
Points: 5937 Visits: 8298
This can be found in BOL:

1) IF UPDATE(b) OR UPDATE(c) ...

2) IF ( COLUMNS_UPDATED() & 2 = 2 )

Best,

Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru at GMail
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search