Web design and hosting, database, cloud and social media solutions that deliver business results
  • Business Solutions
    • Robotic Process Automation
    • Software
    • Database Consultancy Services
      • Data Integration
      • Datawarehouse Services
      • Power BI
      • Server Upgrade and DBA Services
    • Web Site Design Services
      • Logo Design
      • Payment Gateways
      • Web Localisation and Translation
      • Web Site Optimisation
      • Web Site Security
      • Technical Tools
    • Cloud Services
      • Amazon Web Services
      • Google Cloud Services
      • Microsoft Azure
    • Microsoft Office
    • Social Media Management and Advice Services
  • Academy
    • Our Test Environment
    • Learning Databases
      • The Basics
      • Get Open Query
      • SQL Server Data
      • SQL Server Maintenance
      • Using SQL Server Dates
      • Using SQL Server Functions
      • Using SQL Server Pivot-Unpivot
      • Technical Tools
    • Learning Web Design
      • Building Ousia Content Management System
      • Using ASP-NET
      • Using CSS
      • Using JavaScript
    • Learning Cloud and IT Services
      • Task Scheduler Error 2147943645
      • Requesting SSL and Generation of PFX file in OpenSSL Simple Steps
    • Using Social Media
      • Asking for a Google Review
      • Changing a Facebook account from personal to business
      • Choosing where to focus Social Media effort
      • Social Media Image Sizes
      • Using Meta Data to set Social Media Images
  • About Us
    • Blog
      • Building an entry level gaming machine
      • Google Core Update Jan 2020
      • Hot Chilli Internet Closure
      • How To Choose Content For Your Website Adverts Leaflets
      • Preventing Online Scam
      • Skimmers of the gig economy
      • The most annoying things about websites on the Internet
      • Top 5 websites for free Vector Graphics
    • Careers
      • Translator English-Portuguese
      • Translator English-Spanish
    • Portfolio
    • Team
      • Adrian Anandan
      • Ali Al Amine
      • Ayse Hur
      • Chester Copperpot
      • Deepika Bandaru
      • Gavin Clayton
      • Sai Gangu
      • Suneel Kumar
      • Surya Mukkamala
عربى (AR)čeština (CS)Deutsch (DE)English (EN-GB)English (EN-US)Español (ES)فارسی (FA)Français (FR)हिंदी (HI)italiano (IT)日本語 (JA)polski (PL)Português (PT)русский (RU)Türk (TR)中国的 (ZH)

Auditing and synchronising data in cross database using a trigger

Auditing and synchronising tables in different databases, with different data structure using a trigger

About

This is a very stripped down version of some code that we set up at a previous client site. They had two very different databases on different servers (customer and dialler) that needed to have certain data syncronised in real time.

There were a couple of ways of doing it, replication, or stored procedures linked to a job or triggers, in their example it had to be a job, because we didn't own the source code for one of the databases, however my preferred method would be using triggers with something like this...

SQL

USE ClaytabaseAcadamyGOCREATE TABLE Customer(CustomerID INT IDENTITY(1,1) CONSTRAINT PK_CustomerID PRIMARY KEY,CustomerName NVARCHAR(100),CustomerStatus INT--,Other Customer Data...)CREATE TABLE CustomerAudit(CustomerAuditID INT IDENTITY(1,1) CONSTRAINT PK_CustomerAuditID PRIMARY KEY,CustomerAuditType NVARCHAR(100),CustomerAuditDate DATETIME DEFAULT GETDATE(),CustomerID INT,CustomerName NVARCHAR(100),CustomerStatus INT)     CREATE TABLE Dialler(CustomerID INT CONSTRAINT PK_DiallerCustomerID PRIMARY KEY,CustomerStatus INT,CustomerName NVARCHAR(100),DiallerStatus INT--Other Dialler Records)GO

So now we have created some very basic tables that can store customer data. Next we will create some Stored Procedures to handle updating records on the dialler side.

The reason I have done this as stored procedures is to keep the volume of data down (i don't want server 1 sending database records to server 2), it is in effect pinching what would normally be done in a web environment.

SQL

USE ClaytabaseAcadamyGOCREATE PROC DiallerUpdate(@CustomerID INT,@CustomerName NVARCHAR(100),@CustomerStatus INT) AS BEGINUPDATE Dialler SET CustomerStatus=@CustomerStatus,CustomerName=@CustomerNameWHERE CustomerID=@CustomerIDENDGOCREATE PROC DiallerInsert(@CustomerID INT,@CustomerName NVARCHAR(100),@CustomerStatus INT) AS BEGININSERT INTO Dialler(CustomerID,CustomerName,CustomerStatus,DiallerStatus)SELECT @CustomerID,@CustomerName,@CustomerStatus,0ENDGOCREATE PROC DiallerDelete(@CustomerID INT) AS BEGINDELETE FROM DiallerWHERE CustomerID=@CustomerIDENDGO

Now we have created these, we can move onto creating a trigger that will handle sending the data and as we are doing it, we can also auditing our records.

SQL

USE ClaytabaseAcadamyGOCREATE TRIGGER CustomerInsert ON Customer AFTER INSERTAS BEGINDECLARE @CustomerID INT,@CustomerName NVARCHAR(100),@CustomerStatus INT--Get Record DetailsSELECT @CustomerID=CustomerID,@CustomerName=CustomerName,@CustomerStatus=CustomerStatus FROM inserted--Add to AuditINSERT INTO CustomerAudit(CustomerAuditType,CustomerID,CustomerName,CustomerStatus)SELECT 'Record Created',@CustomerID,@CustomerName,@CustomerStatus--Call Insert ProcedureEXEC dbo.DiallerInsert @CustomerID,@CustomerName,@CustomerStatusENDGOCREATE TRIGGER CustomerUpdate ON Customer AFTER UpdateAS BEGINDECLARE @CustomerID INT,@CustomerName NVARCHAR(100),@CustomerStatus INT--Get Record DetailsSELECT @CustomerID=CustomerID,@CustomerName=CustomerName,@CustomerStatus=CustomerStatus FROM inserted--Add to AuditINSERT INTO CustomerAudit(CustomerAuditType,CustomerID,CustomerName,CustomerStatus)SELECT 'Record Updated',@CustomerID,@CustomerName,@CustomerStatus--Call Update ProcedureEXEC dbo.DiallerUpdate @CustomerID,@CustomerName,@CustomerStatusENDGO CREATE TRIGGER CustomerDelete ON Customer AFTER DELETEAS BEGINDECLARE @CustomerID INT,@CustomerName NVARCHAR(100),@CustomerStatus INT--Get Record DetailsSELECT @CustomerID=CustomerID,@CustomerName=CustomerName,@CustomerStatus=CustomerStatus FROM deleted--Add to AuditINSERT INTO CustomerAudit(CustomerAuditType,CustomerID,CustomerName,CustomerStatus)SELECT 'Record Deleted',@CustomerID,@CustomerName,@CustomerStatus--Call Delete ProcedureEXEC dbo.DiallerDelete @CustomerIDENDGO

And that is pretty much it, we now have an audit of data, and the records on both sides will be syncrised within milliseconds... If it needs to be done cross server, change the EXEC command to {servername}.{databasename}.{schema}.DiallerDelete etc/

Here we can test it.

SQL

USE ClaytabaseAcadamyGO--Insert DataINSERT INTO Customer(CustomerName,CustomerStatus) SELECT ' Name 1',0INSERT INTO Customer(CustomerName,CustomerStatus) SELECT ' Name 2',0INSERT INTO Customer(CustomerName,CustomerStatus) SELECT ' Name 3',0UPDATE Customer SET CustomerStatus=2 WHERE CustomerID=1UPDATE Customer SET CustomerName=' Name 4' WHERE CustomerID=2DELETE FROM Customer WHERE CustomerID=3--Review DataSELECT * FROM DiallerSELECT * FROM CustomerSELECT * FROM CustomerAudit
Selecting from the tables you can now check that each process has done it's job properly. by creating the different processes, it has also allowed us to be a lot more creative with what we have done.

Results

Dialler Records
CustomerIDCustomerStatusCustomerNameDiallerStatus
12Name 10
20Name 40
Customer Records
CustomerIDCustomerNameCustomerStatus
1Name 12
2Name 40
Audit Records
CustomerAuditIDCustomerAuditTypeCustomerIDCustomerNameCustomerStatus
1Record Created1Name 10
2Record Created2Name 20
3Record Created3Name 30
4Record Updated1Name 12
5Record Updated2Name 40
6Record Deleted3Name 30

Author

Was this helpful?

Please note, this commenting system is still in final testing.
Copyright Claytabase Ltd 2020

Registered in England and Wales 08985867

Site Links

RSSLoginLink Cookie PolicySitemap

Social Media

facebook.com/Claytabaseinstagram.com/claytabase/twitter.com/Claytabaselinkedin.com/company/claytabase-ltd

Get in Touch

+15125961417info@claytabase.comClaytabase USA, 501 Congress Avenue, Suite 150, Austin, Texas, 78701, United States

Partnered With

The settings on this site are set to allow all cookies. These can be changed on our Cookie Policy & Settings page.
By continuing to use this site you agree to the use of cookies.
Ousia Logo
Logout
Ousia CMS Loader