SQL Clone
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
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3259 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
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62814 Visits: 8598
Look at the COLUMNS_UPDATED() clause

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



How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


toddasd
toddasd
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3310 Visits: 3820
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
SSC Guru
SSC Guru (183K reputation)SSC Guru (183K reputation)SSC Guru (183K reputation)SSC Guru (183K reputation)SSC Guru (183K reputation)SSC Guru (183K reputation)SSC Guru (183K reputation)SSC Guru (183K reputation)

Group: General Forum Members
Points: 183141 Visits: 41569
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
SSC Guru
SSC Guru (76K reputation)SSC Guru (76K reputation)SSC Guru (76K reputation)SSC Guru (76K reputation)SSC Guru (76K reputation)SSC Guru (76K reputation)SSC Guru (76K reputation)SSC Guru (76K reputation)

Group: General Forum Members
Points: 76845 Visits: 8874
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 on googles mail service
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