Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQL Trigger for multiple columns - Insert / update Expand / Collapse
Author
Message
Posted Wednesday, December 5, 2012 3:00 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, September 26, 2014 7:36 AM
Points: 229, Visits: 730
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
---
---
--

.
Post #1392869
Posted Wednesday, December 5, 2012 3:34 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Monday, October 13, 2014 6:42 AM
Points: 5,216, Visits: 5,111
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
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

Post #1392884
Posted Wednesday, December 5, 2012 7:06 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 2:16 PM
Points: 477, Visits: 3,673
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.
Post #1392996
Posted Wednesday, December 5, 2012 7:12 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:11 PM
Points: 12,905, Visits: 32,161
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1392999
Posted Thursday, December 6, 2012 6:50 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 2:09 PM
Points: 4,400, Visits: 6,261
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
Post #1393490
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse