Two SqlServer data synchronization solution

Using the database data synchronization update replication 
        replication concept 
        replication is a set of data copied from one data source to multiple data sources in the art, it is an effective way to publish data on a plurality of storage sites. The use of replication technology, users can publish a copy data to multiple servers, so that a different server users can share this data within the scope of the licensing authority. Replication technology ensures that the data in different locations automatically synchronized updates to ensure data consistency.

SQL replication of the basic elements include 
Publishing server, subscription server, distributed servers, publications, articles 
works SQL replication 
SQL SERVER mainly publications, subscription way to handle replication. The source data server where the server is publishing, responsible for published data. Copies of all the changes of the publishing server to post data to the distribution server, the distribution server includes a distributed database, data can be received all the changes and save these changes, and then distributed to subscribers of these changes 
SQL SERVER replication technology type 
SQL SERVER provides three replication technologies are: 
1, snapshot replication (stay will we use this) 
2, transaction replication 
3, merge replication 
as long as the above to clarify these concepts so on the copy and will have a certain degree of understanding. Then we step by step to achieve the replication step. 
The first publication of the first to configure the server 
(1) select the specified [server] node 
(2) from the [Tools] drop-down menu [Copy] sub-menu, select [publish, subscribe to the server and distributed] command 
(3) The system pops up a dialog box point [Next] and then looked up prompts to complete the operation. 
(4) When completed the set later publication server system will add a tree structure for replication monitor server. But also generate a distribution database (distribution) 
Second create a publication 
(1) select the specified server 
(2) sub-menu, select [Create and Manage Publications] command from the [Copy] [Tools] menu. At this point the system will pop up a dialog box 
(3) choose to create a publications database, and then click [Create Publication] 
(4) In the [Create Publication Wizard] prompt dialog box, click [Next] system will pop up a dialog box. The contents of the box are three types of replication. We now select the first snapshot is the default (the other two you can go and see help)  
(5) Click [Next] system requirements specified can subscribe to the release of the database server type, SQLSERVER allows different databases The data replication between ORACLE or ACCESS. But here we choose to run "SQL SERVER 2000" database server 
(6) Click [Next] system will pop up a dialog box that is the definition of the article published in the table to select 
(7) then [Next] until the operation is complete . When you create a publication after publication of creating a database it will become a shared database. 
The third design subscriptions 
(1) select the specified subscriber 
(2) From [Tools] drop-down menu [Copy] sub-menu [subscription request] 
(3) in accordance click [Next] until the operating system will be prompted to check the SQL SERVER agent service running state, a prerequisite for the implementation of copy operation is SQL SERVER agent service must have been launched. 
(4) Click [Finish]. Subscribe to complete the operation. 
Completion of the above steps actually copy is successful. But how to know whether to copy the success of it? Here you can quickly see whether the success of this method. Expand the server following the publication copy - publish content - Right publish content - Properties - hit live - state and then immediately run the agent then hit live agent properties point to dispatch schedule set to occur every day, every minute between 0:00:00 and 23:59:59. The next step is to determine whether to replicate the success of the open C: Program FilesMicrosoft SQL ServerMSSQLREPLDATAuncXIAOWANGZI_database_database below to see if they have some time as the file name of the folder in almost a minute to produce a. If you do not believe, then you open your database look to see whether you have just seen the table published in the specified subscription database subscription server - 
a manual synchronization program 
- timing synchronization data on the server 
- example: 
- - test environments, SQL Server2000, remote server name: xz, user name: sa, no password, test database: the test 
- tables on the server (query analyzer to connect to the server to create) 
the create the table [the user] (the above mentioned id int Key Primary, Number VARCHAR (. 4), name VARCHAR (10)) 
Go 
- the following LAN (local operation) 
- the machine table, state Description: null indicates new record, represents a modified record, 0 indicates no change recording 
if exists (select * from dbo.sysobjects where  id = object_id (N '[user]') and OBJECTPROPERTY (id, N'IsUserTable ') = 1)
Table drop [User] 
the GO 
Create Table [User] (int ID Identity (1,1), Number VARCHAR (. 4), name VARCHAR (10), state 'bit) 
Go 
- create triggers, maintaining the value of the state field 
create trigger ON t_state [User] 
After Update 
AS 
Update [User] SET = State. 1 
from [User] A = the Join inserted The B ON a.id b.id 
WHERE IS Not null a.state 
Go 
- sync for convenience, create a linked server to be synchronized to the server 
- a remote server named here: xz, user name: sa, no password 
IF EXISTS (SELECT. 1 in master..sysservers from srvname = wHERE 'srv_lnk') 
Exec sp_dropserver 'srv_lnk', 'droplogins' 
Go 
Exec the sp_addlinkedserver 'srv_lnk', '', 'the SQLOLEDB', 'the xz' 
Exec sp_addlinkedsrvlogin 'srv_lnk', 'false', null, 'SA' 
Go 
- to create stored procedures sync 
EXISTS IF (SELECT * WHERE from dbo.sysobjects ID = object_id (N '[the dbo]. [p_synchro]') and the OBJECTPROPERTY (ID, N'IsProcedure ') =. 1) 
drop Procedure [the dbo]. [p_synchro] 
the GO 
Create proc p_synchro 
AS 
the --set XACT_ABORT ON 
- start the remote server MSDTC service 
--exec master..xp_cmdshell 'isql / S "xz " / U "sa" / P "" / q "exec master..xp_cmdshell' 'net start MSDTC '', no_output " ', no_output 
- start the native MSDTC service 
--exec master..xp_cmdshell' start MSDTC NET ', no_output 
- distributed transaction, if the table is a primary key column of the mark, with the following method 
DISTRIBUTED TRANSACTION --BEGIN 
- delete data synchronization 
. srv_lnk.test.dbo the delete from [the User] 
the WHERE the above mentioned id not in (the above mentioned id from the SELECT [the User]) 
- new data synchronization 
insert into srv_lnk.test.dbo [. user] 
ID SELECT, Number, name from [User] WHERE IS State null 
- data synchronous modification 
Update srv_lnk.test.dbo [User] SET. 
Number = b.number, name = b.name 
from srv_lnk.test.dbo [. User] a 
the Join [User] B = ON a.id b.id 
WHERE b.state. 1 = 
- synchronous machine according to the present flag updated 
update [User] SET = 0 WHERE ISNULL State (State,. 1). 1 = 
- TRAN a COMMIT 
Go 
- creating a job, the timing synchronize data stored procedure 
if exists (SELECT 1 from msdb..sysjobs where name = ' data processing') 
the eXECUTE msdb.dbo.sp_delete_job job_name @ = 'data processing' 
Exec the msdb .. sp_add_job @ job_name = 'data processing' 
- create a job step 
DECLARE @sql VARCHAR (800), @ dbname VARCHAR (250) 
SELECT @ SQL = 'Exec p_synchro' - data processing command 
, @ dbname = db_name () - the name of the database to perform data processing 
exec msdb..sp_add_jobstep @ job_name = 'Data Processing', 
@step_name = 'data synchronization', 
@subsystem = 'TSQL', 
@ @ dbname = database_name, 
@Command = @sql, 
@retry_attempts =. 5, - the number of retries 
@retry_interval = 5 - retry interval 
- to create a schedule 
EXEC msdb..sp_add_jobschedule @job_name = 'data processing',  
the @name = 'schedule', 
@freq_type. 4 =, - daily 
@freq_interval = 1, - a day perform a 
@active_start_time = 00000 --0 point execution 
go

Guess you like

Origin www.cnblogs.com/SyncNavigator8-4-1/p/10974400.html