Re: Trigger, Identify Insert, Update AND delete in one



seanD (sean.denney@xxxxxxxxx) writes:
Sorry for the newbie question, but I would like to simply populate a
tracking table with 'I', 'U' or 'D' (and the PK id) based on the type
of change being made to the table with a trigger. My question is: Do
I have to query all the psuedo tables to do that? ? Or is there an
easier or mor efficient way? Do I need to create 3 triggers and do
each seperately? I know the below will not work because updates will
be counted multiple times and it seems like there should be an easier
way than doing some sort of anti join between deleted and inserted.
In Oracle you can use IF inserting... else if updating...else if
deleting...

In SQL Server you can replace this with

IF EXISTS (SELECT * FROM inserted) AND
NOT EXISTS (SELECT * FROM deleted)

For "If inserting..." and similar for the other.

In practice you can write your trigger if you want to save some typing as:

IF EXISTS (SELECT * FROM inserted) AND EXISTS (SELECT * FROM deleted)
BEGIN
INSERT mod_test (id, action)
SELECT id, 'U' FROM deleted
END
ELSE
BEGIN
INSERT mod_test (id, action)
SELECT id, 'U' FROM inserted
UNION ALL
SELECT id, 'D' FROM deleted
END

The alternative is to use Change Data Capture, which is a new feature
in SQL 2008, which uses the same mechanisms as replication, and is
asynchronous. It's probably more work to set up, but may cause less
contention than using a trigger.


--
Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

.



Relevant Pages

  • Re: application roles
    ... I 've made a trigger on a SQL2000 database to test your solution with the ... But my login-account and database user appear in my logtable instead of the ... if you are on SQL 2005. ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ...
    (microsoft.public.sqlserver.security)
  • Re: Update trigger
    ... nvltrigger for each column for example. ... SQL> drop trigger emp_upd_trig ... SQL> create table emp ( ... Commit complete. ...
    (comp.databases.oracle.server)
  • Re: Transaction Abort in a trigger SQL 2008, writing to event log
    ... creating another transaction within the trigger. ... Links for SQL Server Books Online: ...
    (microsoft.public.sqlserver.programming)
  • Re: Raising an event when a row is added
    ... Are you inserting the row from your code? ... event from inside Sql Server that can be consumed by a .Net class. ... might want to make a trigger on the inserted table that updates another ... You could then periodically poll ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Update trigger
    ... nvltrigger for each column for example. ... record update even if we are only effectively changing one column ... SQL> create or replace trigger emp_upd_trig ... SQL> update emp ...
    (comp.databases.oracle.server)