Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query on Trigger

Author  Topic 

pvccaz
Yak Posting Veteran

87 Posts

Posted - 2013-01-31 : 14:52:14
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
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2013-01-31 : 15:02:56
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

87 Posts

Posted - 2013-01-31 : 17:08:56
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
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2013-01-31 : 18:07:15
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

87 Posts

Posted - 2013-01-31 : 18:19:47
Great !. Thanks for you help !
Go to Top of Page
   

- Advertisement -