Web design and hosting, database, cloud and social media solutions that deliver business results
  • Soluzioni aziendali
    • automazione dei processi robotici
    • Software
    • Servizi database
      • Aggiornamento del server e servizi DBA
      • Integrazione dei dati
      • Power BI
      • Servizi di Datawarehouse
    • Sito Web Design
      • Design del logo
      • Gateway di pagamento
      • Localizzazione e traduzione web
      • Ottimizzazione del sito web
      • Sicurezza del sito
      • Strumenti tecnici
    • Servizi per gli affari
      • Microsoft Azure
      • Servizi Google Cloud
      • Servizi Web Amazon
    • Microsoft Office
    • Servizi di consulenza e gestione dei social media
  • Accademia
    • Il nostro ambiente di prova
    • Imparare a usare i database
      • Le basi
      • Ottieni una query aperta
      • Piano di manutenzione di SQL Server 2008
      • Utilizzo dei dati di SQL Server
      • Utilizzo delle date di SQL Server
      • Utilizzo delle funzioni di SQL Server
      • Utilizzo di SQL Server Pivot-Unpivot
      • Strumenti
    • Imparare il web design
      • Costruire il sistema di gestione dei contenuti di Ousia
      • ASP-NET
      • CSS
      • Utilizzo di JavaScript
    • Usando i social media
      • Chiedere una recensione su Google
      • Dimensioni delle immagini dei social media
      • Modifica di un account Facebook da personale a aziendale
      • Scegliere dove concentrare lo sforzo sui social media
      • Utilizzo dei metadati per impostare le immagini dei social media
    • Imparare a usare il cloud e i servizi informatici
      • Errore dell'utilità di pianificazione 2147943645
      • Richiesta SSL e generazione di file PFX in OpenSSL Simple Steps
  • Chi Siamo
    • Carriere
      • Traduttore inglese-portoghese
      • Traduttore inglese-spagnolo
    • Portfolio
    • Squadra
      • Adrian Anandan
      • Alì Al Amine
      • Ayse Hur
      • Chester Copperpot
      • 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)

Controllo e sincronizzazione dei dati in cross database utilizzando un trigger

Controllo e sincronizzazione di tabelle in diversi database, con diversa struttura dei dati tramite un trigger

Di

Questa è una versione molto ridotta di un codice che abbiamo impostato su un precedente sito client. Avevano due database molto diversi su server diversi (cliente e dialer) che dovevano sincronizzare determinati dati in tempo reale.

C'erano un paio di modi per farlo, la replica o le procedure memorizzate collegate a un lavoro o trigger, nel loro esempio doveva essere un lavoro, perché non possedevamo il codice sorgente per uno dei database, tuttavia il mio preferito il metodo utilizzerebbe trigger con qualcosa del genere ...

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

È stato utile?

Quindi ora abbiamo creato alcune tabelle molto semplici che possono memorizzare i dati dei clienti. Successivamente creeremo alcune stored procedure per gestire l'aggiornamento dei record sul lato dialer.

Il motivo per cui l'ho fatto come stored procedure è per mantenere basso il volume dei dati (non voglio che il server 1 invii record di database al server 2), in effetti sta pizzicando ciò che normalmente verrebbe fatto in un ambiente web.

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

È stato utile?

Ora che li abbiamo creati, possiamo passare alla creazione di un trigger che gestirà l'invio dei dati e mentre lo stiamo facendo, possiamo anche controllare i nostri record.

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

È stato utile?

E questo è praticamente tutto, ora abbiamo un controllo dei dati e i record su entrambi i lati verranno sincronizzati in pochi millisecondi... Se è necessario farlo su più server, cambia il comando EXEC in {servername}.{databasename} .{schema}.DiallerDelete ecc/

Qui possiamo testarlo.

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

Autore

È stato utile?

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

Registered in England and Wales 08985867

Site Links

RSSLoginLink Cookie PolicyMappa del sito

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

Partnered With

Le impostazioni di questo sito sono impostate per consentire tutti i cookie. Questi possono essere modificati sulla nostra pagina politica e le impostazioni dei cookie. Continuando a utilizzare questo sito l'utente accetta l'utilizzo dei cookie.
Ousia Logo
Logout
Ousia CMS Loader