Web design and hosting, database, cloud and social media solutions that deliver business results
  • خدمات مشاوره پایگاه داده
    • ابزارها
      • پشتیبان کد SQL
      • چاپگر SQL
    • مقالات فنی
      • استفاده از SQL Server Dates
      • استفاده از توابع سرور SQL
      • با استفاده از SQL Server Pivot-Unpivot
      • تعمیر و نگهداری سرور SQL
      • داده SQL سرور
  • راه حل های کسب و کار
  • طراحی وب سایت
    • جزیره وایت
    • مقالات فنی
  • خدمات شغلی
  • رسانه های اجتماعی
  • آکادمی
  • دربارهی ما
    • تیم
      • چستر Copperpot
      • سونیل کومار
    • نمونه کارها
عربى (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)

حسابرسی و همگام سازی داده ها در پایگاه داده متقابل با استفاده از یک ماشه

حسابرسی و همگام سازی جداول در پایگاه داده های مختلف ، که می تواند با ایجاد یک ماشه ساختار کمی متفاوت داشته باشد

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

CREATE 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

CREATE PROC DiallerUpdate(@CustomerID INT,@CustomerName NVARCHAR(100),@CustomerStatus INT) AS BEGIN
UPDATE Dialler SET CustomerStatus=@CustomerStatus,CustomerName=@CustomerName
WHERE CustomerID=@CustomerID
END
GO
CREATE PROC DiallerInsert(@CustomerID INT,@CustomerName NVARCHAR(100),@CustomerStatus INT) AS BEGIN
INSERT INTO Dialler(CustomerID,CustomerName,CustomerStatus,DiallerStatus)
SELECT @CustomerID,@CustomerName,@CustomerStatus,0
END
GO
CREATE PROC DiallerDelete(@CustomerID INT) AS BEGIN
DELETE FROM Dialler
WHERE CustomerID=@CustomerID
END
GO

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

CREATE TRIGGER CustomerInsert ON Customer AFTER INSERT
AS BEGIN
DECLARE @CustomerID INT,@CustomerName NVARCHAR(100),@CustomerStatus INT
--Get Record Details
SELECT @CustomerID=CustomerID,@CustomerName=CustomerName,@CustomerStatus=CustomerStatus FROM inserted
--Add to Audit
INSERT INTO CustomerAudit(CustomerAuditType,CustomerID,CustomerName,CustomerStatus)
SELECT 'Record Created',@CustomerID,@CustomerName,@CustomerStatus
--Call Insert Procedure
EXEC dbo.DiallerInsert@CustomerID,@CustomerName,@CustomerStatus
END
GO
CREATE TRIGGER CustomerUpdate ON Customer AFTER Update
AS BEGIN
DECLARE @CustomerID INT,@CustomerName NVARCHAR(100),@CustomerStatus INT
--Get Record Details
SELECT @CustomerID=CustomerID,@CustomerName=CustomerName,@CustomerStatus=CustomerStatus FROM inserted
--Add to Audit
INSERT INTO CustomerAudit(CustomerAuditType,CustomerID,CustomerName,CustomerStatus)
SELECT 'Record Updated',@CustomerID,@CustomerName,@CustomerStatus
--Call Update Procedure
EXEC dbo.DiallerUpdate@CustomerID,@CustomerName,@CustomerStatus
END
GO 
CREATE TRIGGER CustomerDelete ON Customer AFTER DELETE
AS BEGIN
DECLARE @CustomerID INT,@CustomerName NVARCHAR(100),@CustomerStatus INT
--Get Record Details
SELECT @CustomerID=CustomerID,@CustomerName=CustomerName,@CustomerStatus=CustomerStatus FROM deleted
--Add to Audit
INSERT INTO CustomerAudit(CustomerAuditType,CustomerID,CustomerName,CustomerStatus)
SELECT 'Record Deleted',@CustomerID,@CustomerName,@CustomerStatus
--Call Delete Procedure
EXEC dbo.DiallerDelete@CustomerID
END
GO

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

-- Insert Data
INSERT INTO Customer(CustomerName,CustomerStatus) SELECT ' Name 1',0
INSERT INTO Customer(CustomerName,CustomerStatus) SELECT ' Name 2',0
INSERT INTO Customer(CustomerName,CustomerStatus) SELECT ' Name 3',0
UPDATE Customer SET CustomerStatus=2 WHERE CustomerID=1
UPDATE Customer SET CustomerName=' Name 4' WHERE CustomerID=2
DELETE FROM Customer WHERE CustomerID=3
--Review Data
SELECT * FROM Dialler
SELECT * FROM Customer
SELECT * 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

RSSLoginLink سیاست کوکینقشه سایت

Social Media

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

Get in Touch

+442392064871info@claytabase.comClaytabase Ltd, Unit 3d, Rink Road Industrial Estate, PO33 2LT, United Kingdom
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