Example 2: An example using a local pending table

The pending table is a design enhancement of applied and request functions that allows clients at a remote site to update primary data and see the updates at the replicate site before they are returned from the primary site. Use this model to implement local update applications.

In this strategy, a client application at a remote replicate site executes a user stored procedure that updates data at the primary site using a request function. Changes to the primary data are replicated to the remote replicate site via an applied function. A local pending table lets clients at the remote replicate site to see updates that are pending at the replicate site before the replication system returns the updates.

When a client application executes the user stored procedure at the replicate data server, it:

When the update succeeds at the primary database, it is distributed to the remote replicate sites, including the site where the transaction originated. At the replicate site, a stored procedure updates the replicated table and deletes the corresponding updates from the pending table.

To use applied functions, request functions, and a local pending table, you must:

At the replicate site:

At the primary site:

In this example, a client application at the replicate site (Sydney) executes a stored procedure upd_publishers_pubs2_req, which inserts values in the publishers_pend table and causes an associated stored procedure, upd_publishers_pubs2, to execute at the primary site (Tokyo). Execution of upd_publishers_pubs2 at the primary site causes the stored procedure upd_publishers_pubs to execute at the replicate site, which updates the publishers table and deletes the corresponding information from the publishers_pend table.

Figure C-2 illustrates the data flow when you use applied functions, request functions, and a local pending table. The gray arrows show the flow of the request function delivery. The black arrows show the flow of the applied function delivery.

Figure C-2: Request functions and a local pending table

Figure C-2 illustrates the data flow when you use applied functions, request functions, and a local pending table. The gray arrows show the flow of the request function delivery. The black arrows show the flow of the applied function delivery.

Pending table

This script creates a pending table in the replicate database.

-- Execute this script at Sydney data server
 -- Creates local pending table 
 create table publishers_pend
 (pub_id char(4) not null,
 pub_name varchar(40) null,
 city varchar(20) null,
 state	char(2) null)
 go
 /* end of script */

Stored procedures

The script creates the stored procedure upd_publisher_pubs2 at the primary site.

The following script modifies the upd_publishers_pub2_req stored procedure at the replicate site. The insert into clause tells the replicate Replication Server to insert values into the publishers_pend table.

-- Execute this script at Sydney data server
 -- Creates stored procedure 
 create procedure upd_publishers_pubs2_req
 (@pub_id char(4),
 @pub_name varchar(40),
 @city varchar(20),
 @state char(2))
 as
 insert into publishers_pend
 values
    (@pub_id, @pub_name, @city, @state)
 go
 /* end of script */

This script creates the upd_publishers_pubs2 procedure for the replicate site. It updates the publishers table and deletes the corresponding information from the publishers_pend table.

-- Execute this script at Sydney data server
 -- Creates stored procedure upd_publishers_pubs2
 create procedure upd_publishers_pubs2
 (@pub_id char(4),
 @pub_name varchar(40),
 @city varchar(20),
 @state char(2))
 as
 update publishers
 set
    pub_name = @pub_name,
    city = @city,
    state = @state
 where
    pub_id = @pub_id
 delete from publishers_pend
 where 
    pub_id = @pub_id
 go
 /* end of script */

Function replication definitions

The script “Function replication definition” creates the function replication definition upd_publishers_pubs2_req—for the request function with versions earlier than 15.1 —at the primary site.

This script creates a function replication definition for the publishers table at the primary Replication Server. The replication definition uses the same parameters and datatypes as the stored procedure in the primary database.

-- Execute this script at Tokyo Replication Server
-- Creates replication definition
upd_publishers_pubs2
create function replication definition
upd_publishers_pubs2
with primary at TOKYO_DS.pubs2
(@pub_id char(4),
@pub_name varchar(40),
@city varchar(20),
@state char(2))
go
/* end of script */

Subscription

This script creates a subscription at the replicate Replication Server using the no-materialization method for the replication definition defined at the primary Replication Server.

-- Execute this script at Sydney Replication Server
-- Creates subscription using no-materialization
for upd_publishers_pubs2create subscription upd_publishers_pubs2_sub
for upd_publishers_pubs2
with replicate at SYDNEY_DS.pubs2
without materialization
go
/* end of script */