たとえば、2 台のサーバー (サーバー A およびサーバー B) が、以下の条件を 2 つとも満たしている場合、この 2 台は双方向のトランザクション レプリケーション構成であると言います。
サーバー A のテーブル T1 に加えられた変更がサーバー B のテーブル T1 にレプリケートされます。
サーバー B のテーブル T1 に加えられた変更がサーバー A のテーブル T1 にレプリケートされます。
したがって、サーバー A で行われた変更がサーバー B にレプリケートされますが、その変更がサーバー B からサーバー A に伝達されることはありません。レプリケーションでは、最初に変更が行われたサーバーに変更を送信するかどうかを判断するため、ディストリビュータによってループバック検出メカニズムが使用されます。
SQL Server を実行しているコンピュータに、ディストリビュータ、パブリッシャ、およびサブスクライバを作成します。これを行うには、次の手順を実行します。
データベースを 2 つ作成します。
use master
go
create database test1
go
create database test2
go
NOT FOR REPLICATION オプションを設定した IDENTITY 列を持つテーブルを 2 つ作成します。
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
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
test2 データベースにカスタム ストアド プロシージャを作成します。
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
SQL Server 7.0 で双方向のトランザクション レプリケーションを実装する方法の関連情報を参照するには、以下の「サポート技術情報」 (Microsoft Knowledge Base) をクリックしてください。
300164?
(http://support.microsoft.com/kb/300164/EN-US/
)
INF: How to Set Up an Identity Column on both the Publisher and the Subscriber with Transactional Replication
この資料は米国 Microsoft Corporation から提供されている Knowledge Base の Article ID 820675?
(http://support.microsoft.com/kb/820675/EN-US/
)
(最終更新日 2004-01-21) を基に作成したものです。