SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query on Trigger
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

pvccaz
Yak Posting Veteran

USA
87 Posts

Posted - 01/31/2013 :  14:52:14  Show Profile  Reply with Quote
Hi,

I need help writing a trigger that inserts records into audit table only if a couple of columns gets updated.

I wrote this trigger for testing.

CREATE TRIGGER upd_trg ON table1
FOR UPDATE
IF (UPDATE(col1) OR UPDATE(col2))
BEGIN
INSERT INTO audit_tbl(col1, col2, dt)
SELECT col1, col2, getdate() from inserted
END

But when i update the table1 for a different column from the
client application that uses this table the entire row gets updated in this table1 and this trigger fires.
So i am trying rewrite the trigger to compare the values using the inserted and deleted tables of the trigger and running to problems.

CREATE TRIGGER upd_trg ON table1
FOR UPDATE
IF inserted.col1 <> deleted.col1 OR inserted.col2 <> deleted.col2
INSERT INTO audit_tbl(col1, col2, dt)
SELECT col1, col2, getdate() from inserted

I am getting error in this one. Can somebody help in pointing me in the correct way of writing this.

Thanks

TG
Flowing Fount of Yak Knowledge

USA
5932 Posts

Posted - 01/31/2013 :  15:02:56  Show Profile  Reply with Quote
inserted and deleted may have many rows so try this. And if any of the columns are NULL-able then you'll need to coalesce or isnull the values to compare in the WHERE clause because you can't compare NULL to anything.

INSERT INTO audit_tbl(col1, col2, dt)
SELECT i.col1, i.col2, getdate() 
from inserted i
inner join deleted d on d.PKs = i.PKs
where  not (
              i.col1 = d.col1
       and    i.col2 = d.col2
       )


Be One with the Optimizer
TG
Go to Top of Page

pvccaz
Yak Posting Veteran

USA
87 Posts

Posted - 01/31/2013 :  17:08:56  Show Profile  Reply with Quote
Prefect !!. Thanks !! That worked.

I also want to insert a record into the audit table and added FOR UPDATE, INSERT and checked for the deleted counts and insereted counts. Modified script below and it works

Is there any other good way to check if insert happened in that table and then insert into audit table


CREATE TRIGGER trg1 ON Tbl1
FOR UPDATE, INSERT
AS

BEGIN
INSERT INTO audit_tbl(col1, col2, dt)
SELECT i.col1, i.col2, getdate()
from inserted i
inner join deleted d on d.PKs = i.PKs
where not (
i.col1 = d.col1
and i.col2 = d.col2
)

END

BEGIN
DECLARE @insertcount int
DECLARE @deletecount int
SET @insertcount = (SELECT count(*) from inserted)
SET @deletecount = (SELECT count(*) from deleted)
IF @insertcount > 0 AND @deletecount = 0
BEGIN
INSERT INTO audit_tbl(col1, col2, dt)
SELECT i.col1, i.col2, getdate()
from inserted i
END

END
Thanks
Go to Top of Page

TG
Flowing Fount of Yak Knowledge

USA
5932 Posts

Posted - 01/31/2013 :  18:07:15  Show Profile  Reply with Quote
use my query but change it to LEFT OUTER JOIN to deleted. Then you'll know if it is an insert if the deleted PK values are NULL. Something like this:

INSERT INTO audit_tbl(col1, col2, dt, userAction)
SELECT i.col1, i.col2, getdate(), case when d.PK is null then 'I' else 'U' end 
from inserted i
inner join deleted d on d.PKs = i.PKs
where  d.PK is null
OR     not (
              i.col1 = d.col1
       and    i.col2 = d.col2
       )


Be One with the Optimizer
TG
Go to Top of Page

pvccaz
Yak Posting Veteran

USA
87 Posts

Posted - 01/31/2013 :  18:19:47  Show Profile  Reply with Quote
Great !. Thanks for you help !
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000