Step 6: Test the Application
New Information - September 2001.
To test the application you have built, execute updates and then select the data to see the results. Use the datecol column to see when rows were updated at each database, relative to each other.
Examples
1. Execute updates to the first row in test1 and test2
USE test1
GO
UPDATE two_way_test1 SET intcol = 20 , charcol = 'updated at test1' WHERE pkcol = 1
USE test2
GO
UPDATE two_way_test2 SET intcol = 60 , charcol = 'updated at test2' WHERE pkcol = 1
2. Select data from both tables to verify that the changes were propagated
SELECT * FROM test1..two_way_test1 WHERE pkcol = 1
SELECT * FROM test2..two_way_test2 WHERE pkcol = 1
If the starting value of intcol was 10, the value in test1 was incremented by 10 and the value in test2 was incremented by 50. The conflict resolution in the update custom procedure sums the values in this column, so this integer value converges to 70 in both databases. It may take a few seconds to see the results in both tables.
Step 4: Create Subscriptions with Cycle Detection Enabled
New Information - September 2001.
To create subscriptions with cycle detection enabled, add a transactional subscription to each database with @loopback_detection set to TRUE, so that the Distribution Agent does not send transactions that originated at the Subscriber back to the Subscriber.
Examples
1. Add the transactional subscription in test1
USE test1
GO
Step 5: Create Custom Stored Procedures to Apply Changes and Handle Conflicts
New Information - September 2001.
The update procedures in the examples have been customized to detect and handle simple conflicts. If a conflict is detected in the intcol column, the current value and new increment are added together; if a conflict is detected in the charcol field, the values are concatenated together.
Examples
1. Create custom stored procedures in test1
USE test1
GO
IF EXISTS (SELECT * FROM sysobjects WHERE name LIKE 'sp_ins_two_way_test1' and type = 'P')
DROP proc sp_ins_two_way_test1
IF EXISTS (SELECT * FROM sysobjects WHERE name LIKE 'sp_upd_two_way_test1' and type = 'P')
DROP proc sp_upd_two_way_test1
IF EXISTS (SELECT * FROM sysobjects WHERE name LIKE 'sp_del_two_way_test1' and type = 'P')
DROP proc sp_del_two_way_test1
GO
-- Insert procedure
CREATE proc sp_ins_two_way_test1 @pkcol int,
@intcol int,
@charcol char(100),
@datecol datetime
AS
INSERT INTO two_way_test1 (pkcol, intcol, charcol,
datecol)
VALUES (@pkcol, @intcol, @charcol, GETDATE())
GO
UPDATE two_way_test1 SET intcol = @intcol,
charcol = @charcol, datecol = GETDATE()
WHERE pkcol = @old_pkcol
GO
-- Delete procedure
CREATE proc sp_del_two_way_test1 @old_pkcol int,
@old_intcol int,
@old_charcol char(100),
@old_datecol datetime
AS
DELETE two_way_test1 WHERE pkcol = @old_pkcol
GO
2. Create custom stored procedures in test2
USE test2
GO
IF EXISTS (SELECT * FROM sysobjects WHERE name LIKE 'sp_ins_two_way_test2' and type = 'P')
DROP proc sp_ins_two_way_test2
IF EXISTS (SELECT * FROM sysobjects WHERE name LIKE 'sp_upd_two_way_test2' and type = 'P')
DROP proc sp_upd_two_way_test2
IF EXISTS (SELECT * FROM sysobjects WHERE name LIKE 'sp_del_two_way_test2' and type = 'P')
DROP proc sp_del_two_way_test2
GO
-- Insert procedure
CREATE proc sp_ins_two_way_test2 @pkcol int,
@intcol int,
@charcol char(100),
@datecol datetime
AS
INSERT INTO two_way_test2 (pkcol, intcol, charcol,datecol)
VALUES (@pkcol, @intcol, @charcol, GETDATE())
GO
Step 3: Create Reciprocal Publications
New Information - September 2001.
To create reciprocal publications, add the transactional publication and article to each database. Note that:
Custom stored procedures are used for @ins_cmd, @del_cmd, and @upd_cmd.
The XCALL style parameters are used in UPDATE and DELETE stored procedures. For more information, see Using Custom Stored Procedures in Articles.
@schema_option disables autogeneration of the default custom stored procedures because they will be created in Step 4.
Examples
1. Add the transactional publication and article in test1
USE test1
GO
完全同步不好做,如果操作很频繁会很慢。
可以用触发器,如果完全同步可以先建立linkserver然后update db1_server.db1.dbo.tab1 set ... from updated
但这样慢,可以在DB1上建立修改存储过程 update tab1 set col4 = b.col4 from tab1, db2_server.db2.dbo.tab1 b where tab1.key = b.key
和插入存储过程 insert .... 然后在再第一个机器的数据库的表的触发器里调用远程存储过程。
如果不要完全同步可以先用触发器写到本地的表中(建立一个结构一样的trans表,增加一标志字段,和一传输字段)然后写任务,半小时或多久传一次
Step 1: Create the Databases and Enable Replication
New Information - September 2001.
To set up bidirectional replication
Create the two databases for your application: test1 and test2.
Enable the server as a Publisher, Subscriber, and Distributor. If the server is already enabled as a Publisher, Subscriber, and Distributor, you can skip this step.
Enable the databases for replication.
Examples
1. Create the test databases
CREATE database test1
CREATE database test2
2. Enable the server as a Publisher, Subscriber, and Distributor
EXEC master..sp_adddistributor @distributor = @@SERVERNAME
GO
EXEC master..sp_adddistributiondb @database= 'distribution'
GO
EXEC master..sp_adddistpublisher @publisher = @@SERVERNAME, @distribution_db = 'distribution', @working_directory = 'C:\Program Files\Microsoft SQL Server\MSSQL\REPLDATA'
GO
EXEC master..sp_addsubscriber @subscriber = @@SERVERNAME, @type = 0, @security_mode = 1
GO
EXEC master..sp_changesubscriber_schedule @subscriber = @@SERVERNAME, @agent_type = 1, @active_end_date = 0
GO
3. Enable the databases for replication
USE master
GO
EXEC sp_replicationdboption N'test1', N'publish', true
GO
EXEC sp_replicationdboption N'test2', N'publish', true
GO
Step 2: Create a Bidirectional Schema
New Information - September 2001.
To create a bidirectional schema, create a table with the same schema in each test database and populate it with sample data.
Examples
1. Create a table in test1 and populate with 10 rows
USE test1
GO
IF EXISTS (SELECT * FROM sysobjects WHERE name LIKE 'two_way_test1')
DROP TABLE two_way_test1
GO
CREATE TABLE two_way_test1
(pkcol int primary key not null,
intcol int,
charcol char(100),
datecol datetime
)
GO
INSERT INTO two_way_test1 VALUES (1, 10, 'row1', GETDATE())
INSERT INTO two_way_test1 VALUES (2, 20, 'row2', GETDATE())
INSERT INTO two_way_test1 VALUES (3, 30, 'row3', GETDATE())
INSERT INTO two_way_test1 VALUES (4, 40, 'row4', GETDATE())
INSERT INTO two_way_test1 VALUES (5, 50, 'row5', GETDATE())
INSERT INTO two_way_test1 VALUES (6, 60, 'row6', GETDATE())
INSERT INTO two_way_test1 VALUES (7, 70, 'row7', GETDATE())
INSERT INTO two_way_test1 VALUES (8, 80, 'row8', GETDATE())
INSERT INTO two_way_test1 VALUES (9, 90, 'row9', GETDATE())
INSERT INTO two_way_test1 VALUES (10, 100, 'row10', GETDATE())
GO
2. Create a table in test2 and populate with 10 rows
USE test2
GO
IF EXISTS (SELECT * FROM sysobjects WHERE name LIKE 'two_way_test2')
DROP TABLE two_way_test2
GO
CREATE TABLE two_way_test2
(pkcol int primary key not null,
intcol int,
charcol char(100),
datecol datetime
)
GO
INSERT INTO two_way_test2 VALUES (1, 10, 'row1', GETDATE())
INSERT INTO two_way_test2 VALUES (2, 20, 'row2', GETDATE())
INSERT INTO two_way_test2 VALUES (3, 30, 'row3', GETDATE())
INSERT INTO two_way_test2 VALUES (4, 40, 'row4', GETDATE())
INSERT INTO two_way_test2 VALUES (5, 50, 'row5', GETDATE())
INSERT INTO two_way_test2 VALUES (6, 60, 'row6', GETDATE())
INSERT INTO two_way_test2 VALUES (7, 70, 'row7', GETDATE())
INSERT INTO two_way_test2 VALUES (8, 80, 'row8', GETDATE())
INSERT INTO two_way_test2 VALUES (9, 90, 'row9', GETDATE())
INSERT INTO two_way_test2 VALUES (10, 100, 'row10', GETDATE())
GO