附註每個部署可能需要以不同的方法來解決這些衝突根據商務需求而定。這些衝突也比較容易解決當涉及只有兩個伺服器。當牽涉到兩個以上的伺服器就可以使用預存程序來判斷哪個伺服器發出所做的變更。用來更新伺服器 C 中的資料錄更新預存程序並不會知道變更是否來自伺服器 A 或在伺服器 B。與合併複寫不同的是交易式複寫不被設計來解決衝突。部署交易式複寫,而不是可避免衝突的案例中僅解決。
在執行 SQL Server 的電腦上建立散發者、 發行者和 「 訂閱者 」。如果要執行這項操作,請依照下列步驟執行:
建立兩個資料庫:
use master
go
create database test1
go
create database test2
go
建立具有 IDENTITY 資料行設定 NOT FOR 複寫選項的兩個資料表:
use test1
go
create table two_way_test1
(
pkcol INTEGER PRIMARY KEY NOT NULL,
intcol INTEGER IDENTITY(1,1) NOT FOR REPLICATION,
charcol CHAR(100),
timestampcol TIMESTAMP
)
use test2
go
create table two_way_test2
(
pkcol INTEGER PRIMARY KEY NOT NULL,
intcol INTEGER IDENTITY(1000000000,1) NOT FOR REPLICATION,
charcol CHAR(100),
timestampcol TIMESTAMP
)
go
-- Constraint to enforce a range of values between 1 and 1000 in database test1
use test1
go
alter table
two_way_test1
with nocheck
add constraint
checkprimcol check NOT FOR REPLICATION
(
pkcol BETWEEN 1 and 1000
)
go
use test2
go
-- Constraint to enforce a range of values between 1001 and 2000 in the database test2
alter table
two_way_test2
with nocheck
add constraint
checkprimcol check NOT FOR REPLICATION
(
pkcol BETWEEN 1001 and 2000
)
go
啟用您的伺服器作為 「 散發者,然後建立散發資料庫:
use master
go
sp_adddistributor @distributor = '<distributor name>'
go
use master
go
sp_adddistributiondb @database='distribution'
go
啟用的所有電腦執行 SQL Server 會參與發行者為複寫:
use master
go
exec sp_adddistpublisher
@publisher = '<Your Server Name>',
@distribution_db ='distribution',
@security_mode = 0,
@login = 'sa',
@password = 'sa',
@working_directory ='<Location of Directory>'
啟用所有已識別的資料庫,供複寫:
use master
go
exec sp_replicationdboption N'test1', N'publish', true
go
exec sp_replicationdboption N'test2', N'publish', true
go
Create the custom stored procedures in the test1 database:
use test1
go
-- INSERT Stored Procedure
create procedure sp_ins_two_way_test1
@pkcol int,
@intcol int,
@charcol char(100),
@timestampcol timestamp,
@rowidcol uniqueidentifier
as
insert into two_way_test1
(
pkcol,
intcol,
charcol
)
values
(
@pkcol,
@intcol,
@charcol
)
go
--UPDATE Stored Procedure
create procedure sp_upd_two_way_test1
@pkcol int,
@intcol int,
@charcol char(100),
@timestampcol timestamp,
@rowidcol uniqueidentifier,
@old_pkcol int
as
declare @x int
declare @y int
declare @z char(100)
select
@x=pkcol,
@y=intcol,
@z=charcol
from
two_way_test1
where
pkcol = @pkcol
delete
two_way_test1
where
pkcol=@pkcol
insert into two_way_test1
(
pkcol,
intcol,
charcol
)
values
(
case isnull(@pkcol,0) when 0 then @x else @pkcol end,
case isnull(@intcol,0) when 0 then @y else @intcol end,
case isnull(@charcol,'N') when 'N' then @z else @charcol end
)
go
-- DELETE Stored Procedure
create procedure sp_del_two_way_test1
@old_pkcol int
as
delete
two_way_test1
where
pkcol = @old_pkcol
go
Create the custom stored procedures in the test2 database:
use test2
go
-- INSERT Stored Procedure
create procedure sp_ins_two_way_test2
@pkcol int,
@intcol int,
@charcol char(100),
@timestampcol timestamp,
@rowidcol uniqueidentifier
as
insert into two_way_test2
(
pkcol,
intcol,
charcol
)
values
(
@pkcol,
@intcol,
@charcol
)
go
--UPDATE Stored Procedure
create procedure sp_upd_two_way_test2
@pkcol int,
@intcol int,
@charcol char(100),
@timestampcol timestamp,
@rowidcol uniqueidentifier,
@old_pkcol int
as
declare @x int
declare @y int
declare @z char(100)
select
@x=pkcol,
@y=intcol,
@z=charcol
from
two_way_test2
where
pkcol = @pkcol
delete
two_way_test2
where
pkcol=@pkcol
insert into two_way_test2
(
pkcol,
intcol,
charcol
)
values
(
case isnull(@pkcol,0) when 0 then @x else @pkcol end,
case isnull(@intcol,0) when 0 then @y else @intcol end,
case isnull(@charcol,'N') when 'N' then @z else @charcol end
)
go
-- DELETE Stored Procedure
create procedure sp_del_two_way_test2
@old_pkcol int
as
delete
two_way_test2
where
pkcol = @old_pkcol
go
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。