Step by step 99-real-time synchronization of different database data

Source: Internet
Author: User
Tags bulk insert

Description

Project Master Data Intermediate table synchronization
Project master data now makes 3 new intermediate tables, our task is to do a Windows service from those 3 intermediate tables pull part of the field data into our old intermediate table

Solution:

First, connect the intermediate table database from the code, use the Bulk Insert method in the C # SQL Server class to insert into the local temporary table uniformly, and then process the temporary table into the destination table in the stored procedure.

1: Write a stored procedure first: implement temporal table [dbo]. [Temp_mdm_corporate] and intermediate table [dbo]. [Z_mdm_corporate] the comparison.

If the data in the intermediate table is consistent with the temporary table, the operation is not performed;

If inconsistent: no data in the intermediate table, insert, update

1.1 Creating a data table

CREATE TABLE [dbo]. [Temp_mdm_corporate] ([Corporatecode] [varchar] ( -) not NULL, [corporatename] [nvarchar] ( -) NULL, [Status] [nvarchar] ( -) NULL, [UpdateTime] [datetime] NULL, [F1] [nvarchar] ( $) NULL, [F2] [nvarchar] ( $) NULL, [F3] [nvarchar] ( $) NULL, [F4] [nvarchar] ( $) NULL, [F5] [nvarchar] ( $) NULL, CONSTRAINT [pk_temp_mdm_corporate] PRIMARY KEY CLUSTERED ([Corporatecode] ASC) with (Pad_index = off, Statistics_norecompute = off, Ignore_dup_key = off, Allow_row_locks = on, Allow_page_locks =On ) On [PRIMARY]) on [PRIMARY] GO CREATE TABLE [dbo]. [Z_mdm_corporate] ([Corporatecode] [varchar] ( -) not NULL, [corporatename] [nvarchar] ( -) NULL, [Status] [nvarchar] ( -) NULL, [UpdateTime] [datetime] NULL, [F1] [nvarchar] ( $) NULL, [F2] [nvarchar] ( $) NULL, [F3] [nvarchar] ( $) NULL, [F4] [nvarchar] ( $) NULL, [F5] [nvarchar] ( $) NULL, CONSTRAINT [pk_z_mdm_corporate] PRIMARY KEY CLUSTERED ([Corporatecode] ASC) with (Pad_index= off, Statistics_norecompute = off, Ignore_dup_key = off, Allow_row_locks = on, Allow_page_locks =On ) On [PRIMARY]) on [PRIMARY] GO
View Code

1.2 Creating a stored procedure

CREATE PROCEDURE sp_updatez_mdm_corporate asbegin       MERGE into z_mdm_corporate as Z       USING temp_mdm_corporate as T on       z.corporatecode=T.corporatecode       When matched then       UPDATE SET z.corporatename=t.corporatename, z.status = t.status,z.updatetime=T. UpdateTime when isn't       matched then       INSERT  VALUES (t.corporatecode,t.corporatename,t.status,t.updatetime , t.f1,t.f2,t.f3,t.f4,t.f5);     
View Code

1.3 Speaking of which, we can create a job in the database and run the stored procedure for a period of time.

1.3.1

1.3.2

1.3.3

2: Use the window service to get data from the project master data into the temporary table temp_mdm_corporate.

Step by step 99-real-time synchronization of different database data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.