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 12»»

Using MERGE for Audting Expand / Collapse
Author
Message
Posted Tuesday, October 2, 2012 4:30 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Sunday, November 16, 2014 7:09 AM
Points: 121, Visits: 467
Hi
I'm looking at using Merge which I've never used. I've achieved Auditing before with an After trigger that writes the old row to the audit table. I'm only interested in writting to the audit table if the values change so use checksum.

create table tblCustomer
(CID int not null primary key,
name varchar(10) not null,
country varchar(15) not null)

insert into tblCustomer values (1, 'Bob', 'UK'), (2, 'Claire', 'USA'), (3,'Terry','France')
create table tblUpdates
(
CID int not null,
name varchar(10) not null,
country varchar(15) not null
)

insert into tblUpdates values (1, 'Bob', 'UK'), (2, 'Claire', 'Mexico'), (3,'Terrance','Spain'),(4,'Abdul','Egypt')

select 'UPDATE' as actiontype, getdate() as AuditDate, name, country
into #tAudit from tblCustomer where 1=2

insert into #tAudit
SELECT Actiontype, AuditDate, name, country FROM
(
MERGE tblCustomer AS target
USING (SELECT * FROM tblUpdates ) AS source
ON (target.cid = source.cid)

WHEN MATCHED
THEN UPDATE SET name = source.name, country = source.country
WHEN NOT matched
THEN INSERT
VALUES (CID,Name, Country)

OUTPUT $action as Actiontype, getdate() as AuditDate,
CASE WHEN $action = 'INSERT' THEN inserted.name ELSE deleted.name END as name,
CASE WHEN $action = 'INSERT' THEN inserted.country ELSE deleted.country END as country,
binary_checksum(inserted.name, inserted.country) as chkNew,
binary_checksum(deleted.name, deleted.country) as ChkOld) as tChanges
WHERE chkNew <> ChkOld

select * from #tAudit

drop table #tAudit
drop table tblCustomer
drop table tblUpdates

Is there a better way to achive this.

Post #1366942
Posted Tuesday, October 2, 2012 4:54 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: 2 days ago @ 9:31 AM
Points: 922, Visits: 2,524
You can use the OUTPUT to insert rows into a table rather than the Outer Select, which is rather cumbersome.

this might be better, and you'd need to double check the code works but theres no reason it shouldnt


MERGE tblCustomer AS target
USING (SELECT * FROM tblUpdates ) AS source
ON (target.cid = source.cid)

WHEN MATCHED
AND (target.name!=source.name
or target.country!=source.country)
THEN UPDATE SET name = source.name, country = source.country
WHEN NOT matched
THEN INSERT
VALUES (CID,Name, Country)

OUTPUT $action
, getdate()
, CASE WHEN $action = 'INSERT' THEN inserted.name ELSE deleted.name END as name
, CASE WHEN $action = 'INSERT' THEN inserted.country ELSE deleted.country END as country
, binary_checksum(inserted.name, inserted.country) as chkNew
,binary_checksum(deleted.name, deleted.country) as ChkOld) as tChanges
INTO #tAudit
WHERE chkNew <> ChkOld

Edit I've just seen that you dont have a clause to update only the rows that have changed, so I've added it to the code this will then only update changed rows and insert new rows.


_________________________________________________________________________
SSC Guide to Posting and Best Practices
Post #1366954
Posted Tuesday, October 2, 2012 5:12 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 6:52 AM
Points: 6,872, Visits: 14,185
If you are using the Enterprise edition of SS2K8, then change data capture is well worth a look.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1366964
Posted Tuesday, October 2, 2012 6:47 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Sunday, November 16, 2014 7:09 AM
Points: 121, Visits: 467
Thank you. Your example is easier to read.

BTW I was restricting to only chnage rows by comparing the checksum values.

I didn't know you could add additional filters after is matched i.e. target.name != source.name



Post #1366993
Posted Tuesday, October 2, 2012 6:55 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: 2 days ago @ 9:31 AM
Points: 922, Visits: 2,524
terry999 (10/2/2012)
Thank you. Your example is easier to read.

BTW I was restricting to only chnage rows by comparing the checksum values.

I didn't know you could add additional filters after is matched i.e. target.name != source.name



Sorry theres a coding error I believe it should read

MERGE tblCustomer AS target
USING (SELECT * FROM tblUpdates ) AS source
ON (target.cid = source.cid)

WHEN MATCHED
AND (target.name!=source.name
or target.country!=source.country)
THEN UPDATE SET name = source.name, country = source.country
WHEN NOT matched
THEN INSERT
VALUES (CID,Name, Country)

OUTPUT $action
, getdate()
, CASE WHEN $action = 'INSERT' THEN inserted.name ELSE deleted.name END
, CASE WHEN $action = 'INSERT' THEN inserted.country ELSE deleted.country END
, binary_checksum(inserted.name, inserted.country)
,binary_checksum(deleted.name, deleted.country)
INTO #tAudit



_________________________________________________________________________
SSC Guide to Posting and Best Practices
Post #1367000
Posted Tuesday, October 2, 2012 7:37 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 10:41 AM
Points: 1,945, Visits: 3,127
Look at audit tools that will capture all the data and not be illegal.

Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1367035
Posted Tuesday, October 2, 2012 12:23 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Sunday, November 16, 2014 7:09 AM
Points: 121, Visits: 467
Thanks Jason

I had made that change.

From now on I will use MERGE to replace Triggers for audit - all in one place.



Post #1367222
Posted Tuesday, October 2, 2012 12:23 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Sunday, November 16, 2014 7:09 AM
Points: 121, Visits: 467
illegal?
Post #1367223
Posted Wednesday, October 3, 2012 8:07 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 9:01 AM
Points: 4,439, Visits: 6,347
terry999 (10/2/2012)
Thanks Jason

I had made that change.

From now on I will use MERGE to replace Triggers for audit - all in one place.



Note that this places the onus on the developer to code the audit stuff for EVERY SINGLE DML OPERATION ON EACH TABLE. A trigger can be written once and will capture ALL DML operations, regardless of provenance. Even if your devs are rigorous, what about that quick "oopsie" data cleanup operation you need to do because someone screwed something up. Will you ALWAYS remember to do the MERGE, even for ad hoc DML?


Best,

Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru at GMail
Post #1367700
Posted Tuesday, October 9, 2012 1:55 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Sunday, November 16, 2014 7:09 AM
Points: 121, Visits: 467
Good point, I'd forgotten that
Post #1370209
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse