Home > Articles > Databases > SQL Server

Maintaining Transactional Replication with SQL Server 2005

Baya Dewald
  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Close WindowBaya Dewald

Baya Dewald

Learn more…

Recommended Practices for Analysis Services 2005/2008 Design
Feb 8, 2010
SQL Server Analysis Services 2005/2008 Administration
Jan 22, 2010
SQL Server 2005 Transactional Replication Agents
Oct 20, 2006
Replicating Code Modules with SQL Server 2005
Sep 15, 2006
Monitoring and Troubleshooting Replication Using SQL Server 2005
Sep 1, 2006
Maintaining Transactional Replication with SQL Server 2005
Aug 18, 2006
Setting Up Transactional Replication with SQL Server 2005
Aug 11, 2006
Monitoring and Tuning SQL Server with Profiler
Mar 3, 2006
Tips and Tricks Within Microsoft Analysis Services
Feb 24, 2006
Case Study of Building a Data Warehouse with Analysis Services (Part Two)
Feb 17, 2006
Case Study of Building a Data Warehouse with Analysis Services (Part One)
Feb 10, 2006
Developers vs. DBAs: Keys to Successful Cohabitation
Oct 6, 2005
SQL Server Transactional Replication Agents
Oct 22, 2004
Replicating Code Modules in SQL Server
Oct 15, 2004
Working with Analysis Services Cubes in SQL Server
Oct 1, 2004
Dimensional Databases: Building A Data Warehouse
Sep 17, 2004
SQL Server Log Shipping
Aug 20, 2004
Maintaining Transactional Replication
Apr 30, 2004
Monitoring and Troubleshooting Transactional Replication
Apr 23, 2004
Setting Up Transactional Replication with SQL Server
Mar 26, 2004
Introduction to Database Replication
Mar 5, 2004
SQL Server: Advantages and Drawbacks of User-Defined Functions
May 16, 2003
SQL Server User-Defined Functions (UDFs)
May 2, 2003
SQL Server String, Cursor, Security and Rowset Functions
Apr 18, 2003
Date, Math and Text Functions in SQL Server 2000
Apr 4, 2003
SQL Server System-Related Functions
Mar 14, 2003
Enhancing SQL Server Functionality with Functions
Feb 21, 2003
Optimizing Transact-SQL Code
Aug 9, 2002
SQL Server: Optimizing Database Performance Through Indexes
Aug 2, 2002
SQL Server: Tuning Database Design
Jul 26, 2002
SQL Server Tuning: Database Maintenance
Jul 19, 2002
Application Performance Tuning
Jul 12, 2002
Options Affecting SQL Server Locking Behavior
May 31, 2002
SQL Server: Blocking Problems
May 24, 2002
SQL Server: Details of Locking
May 17, 2002
SQL Server: Transaction and Locking Architecture
May 10, 2002
SQL Server and OPENXML
May 1, 2002
DTS Tips and Tricks
Mar 8, 2002
The DTS Object Model
Mar 1, 2002
Introduction to Data Transformation Services (DTS)
Feb 22, 2002
Normalizing Name Data in SQL Server
Jan 25, 2002
String Manipulations with SQL Server 2000
Jan 25, 2002
The EXPLICIT Mode of FOR XML
Jan 18, 2002
XML Support in Transact-SQL
Jan 18, 2002
Gathering Data for a Data Warehouse
Jan 11, 2002
Steps Involved in Building a Data Warehouse
Jan 11, 2002
Populating a Data Warehouse with SQL Server 2000
Nov 9, 2001
SQL Server: Determining Whether a Date is a Business Day
Nov 9, 2001

Sorry, this author hasn't posted any blogs.

Administrator's Guide to SQL Server 2005

Like this article? We recommend
Administrator's Guide to SQL Server 2005

Microsoft has introduced some great improvements to replication with SQL Server 2005, alleviating some of the problematic maintenance issues in previous releases. Baya Pavliashvili demonstrates the steps necessary to maintain a typical publication, and explains how new options for delivering data changes can help you tailor a replication solution to your application's needs.

The first article in this series introduced you to replication setup with SQL Server 2005. In a simple scenario, replication setup is straightforward; however, replication does require some maintenance. This article shows you how to maintain replication stored procedures, how to make changes to existing publications, and how to reinitialize subscriptions as applied to SQL Server 2005. As in the first article, some differences between the most current and previous versions of SQL Server are highlighted.

Replication Stored Procedures

By default, replication replaces INSERT, UPDATE, and DELETE commands executed on the publisher with respective stored procedures on the subscriber. Doing so is recommended because stored procedures break larger INSERT, UPDATE, and DELETE commands into smaller chunks—affecting one row at a time. So an UPDATE statement that affects 1,000 rows will be translated into 1,000 executions of the update stored procedure. Changing one row at a time isn’t very efficient, but it reduces the locking impact on the subscriber so that you won’t experience many contention issues.

Unlike previous releases, SQL Server 2005 has several new options for statement delivery, which enable you to customize how replication works—depending on your needs. The options are summarized in the following table.

Statement

Option

Meaning

New in SQL Server 2005

INSERT

Call stored procedure (default)

Translate INSERT statements executed on publisher into stored procedure calls, adding a single row at a time.

No

INSERT

Do not replicate INSERT statements

Do not forward INSERT statements executed on the publisher to the subscriber(s).

Yes

INSERT

INSERT statement

Execute the same INSERT statements on publisher and subscriber.

No

INSERT

INSERT statement without column list

Replicate the INSERT statement without specifying the column list:

insert into [dbo].[DimAccount] 
values (11,9,1164,1160,N’work in progress’,N’Assets’,N’+’,NULL,N’Currency’,NULL)

Yes

UPDATE

Do not replicate UPDATE statements

Do not forward UPDATE statements executed on the publisher to the subscriber(s).

Yes

UPDATE

UPDATE statement

Execute the same UPDATE statements on publisher and subscriber.

No

UPDATE

CALL stored procedure (default)

Translate UPDATE statements executed on publisher into stored procedure calls, changing a single row at a time.

No

UPDATE

MCALL stored procedure

Translate UPDATE statements executed on publisher into stored procedure calls, changing a single row at a time. This option passes the value of the affected column and NULL for unaffected columns, as in the following:

{CALL [sp_MSupd_dboDimAccount] 
(NULL,NULL,NULL,NULL,N’work in process’,NULL,NULL,NULL,NULL,NULL,11,0x1000)}

No

UPDATE

XCALL stored procedure

Translate UPDATE statements executed on publisher into stored procedure calls changing a single row at a time. This option passes the values of all columns, as in the following:

{CALL [sp_MSupd_dboDimAccount] (11, 9, 1164, 1160, 
N’work in process’, N’Assets’, N’+’, NULL,
N’Currency’, NULL, 11, 9, 1164, 1160, 
N’work in progress’, N’Assets’, N’+’, NULL, N’Currency’, NULL)}

No

UPDATE

SCALL stored procedure

Translate UPDATE statements executed on publisher into stored procedure calls, changing a single row at a time. This option passes nothing for nonaffected columns and values of the affected columns, as in the following:

{CALL [sp_MSupd_dboDimAccount] 
(,,,,N’work in process’,,,,,,11,0x1000)}

Yes

DELETE

CALL stored procedure (default)

Translate DELETE statements executed on publisher into stored procedure calls, deleting a single row at a time.

No

DELETE

Do not replicate DELETE statements

Do not forward DELETE statements executed on publisher to the subscriber(s).

Yes

DELETE

DELETE statement

Execute the same DELETE statements on publisher and subscriber.

No

DELETE

XCALL stored procedure

Translate DELETE statements executed on publisher into stored procedure calls, deleting a single row at a time. This option passes the values of all columns, as in the following:

{CALL [sp_MSdel_dboDimAccount] (11,9,1164,1160,N’work in progress’,N’Assets’,N’+’,NULL,N’Currency’,NULL)}

No

Note that although previous versions didn’t support ignoring INSERT, UPDATE, or DELETE commands, you could modify stored procedures created by replication on subscribers to achieve similar functionality. This isn’t a recommended practice, however, because customized replication procedures are not supported. More importantly, even if you modify the replication procedures, the commands would still have to be delivered to the distribution database—they just wouldn’t make data changes on the subscriber. With SQL Server 2005, if you configure INSERT, UPDATE, or DELETE commands to not be replicated, such commands are simply ignored and never even forwarded to the distributor. Furthermore, the respective stored procedures are NOT created on the subscribers.

The text of the replication stored procedures executed on the subscriber(s) hasn’t changed much; the skeleton of the INSERT, UPDATE, and DELETE statements is shown below:

/* 
** DELETE procedure
** passes the primary key as the only parameter 
*/
create procedure [dbo].[sp_MSdel_dboDimAccount] 
@pkc1 int 
as 
begin 
delete [dbo].[DimAccount] where [AccountKey] = @pkc1 

if @@rowcount = 0  
if @@microsoftversion>0x07320000   
exec sp_MSreplraiserror 20598 
end 
/*
** INSERT procedure
*/
create procedure [dbo].[sp_MSins_dboDimAccount] 
 @c1 int,@c2 int,@c3 int,@c4 int,@c5 nvarchar(50),@c6 nvarchar(50),@c7 nvarchar(50),@c8 nvarchar(300),@c9 nvarchar(50),@c10 nvarchar(200)
as 
begin 
insert into "dbo"."DimAccount"( 
 "AccountKey"
,"ParentAccountKey"
,"AccountCodeAlternateKey"
,"ParentAccountCodeAlternateKey"
,"AccountDescription"
,"AccountType"
,"Operator"
,"CustomMembers"
,"ValueType"
,"CustomMemberOptions"
 )
values ( 
 @c1
,@c2
,@c3
,@c4
,@c5
,@c6
,@c7
,@c8
,@c9
,@c10
 ) 
end

/*
** UPDATE procedure
*/

create procedure [dbo].[sp_MSupd_dboDimAccount] 
 @c1 int = null,@c2 int = null,@c3 int = null,@c4 int = null,@c5 nvarchar(50) = null,@c6 nvarchar(50) = null,@c7 nvarchar(50) = null,@c8 nvarchar(300) = null,@c9 nvarchar(50) = null,@c10 nvarchar(200) = null,@pkc1 int
,@bitmap binary(2)
as
begin
update "dbo"."DimAccount" set 
 "ParentAccountKey" = case substring(@bitmap,1,1) & 2 when 2 then @c2 else "ParentAccountKey" end
,"AccountCodeAlternateKey" = case substring(@bitmap,1,1) & 4 when 4 then @c3 else "AccountCodeAlternateKey" end
,"ParentAccountCodeAlternateKey" = case substring(@bitmap,1,1) & 8 when 8 then @c4 else "ParentAccountCodeAlternateKey" end
,"AccountDescription" = case substring(@bitmap,1,1) & 16 when 16 then @c5 else "AccountDescription" end
,"AccountType" = case substring(@bitmap,1,1) & 32 when 32 then @c6 else "AccountType" end
,"Operator" = case substring(@bitmap,1,1) & 64 when 64 then @c7 else "Operator" end
,"CustomMembers" = case substring(@bitmap,1,1) & 128 when 128 then @c8 else "CustomMembers" end
,"ValueType" = case substring(@bitmap,2,1) & 1 when 1 then @c9 else "ValueType" end
,"CustomMemberOptions" = case substring(@bitmap,2,1) & 2 when 2 then @c10 else "CustomMemberOptions" end
where "AccountKey" = @pkc1

if @@rowcount = 0
 if @@microsoftversion>0x07320000
  exec sp_MSreplraiserror 20598
end

If you change the publication—for example by adding columns, changing column data types, or filtering replicated columns, you also need to modify the replication stored procedures. If you run the Snapshot Agent, it will automatically create the updated stored procedures for you; however, you might not always have this luxury. Generating snapshots for publications that support large databases can take a long time, and applying such snapshots at the subscriber can take the same time. If you can’t afford lengthy downtime, and the data on the subscriber doesn’t have to be reapplied, there is a better option for refreshing stored procedures. You can execute the system procedure sp_scriptpublicationcustomprocs on the publisher. This procedure accepts the publication name as the only parameter, so it can be called as follows:

EXECUTE sp_scriptpublicationcustomprocs ’publicationName’

The result will be all INSERT, UPDATE, and DELETE stored procedures that you need to apply on the subscribers.

If you’re using immediate updating subscribers, you can use another system procedure to script the triggers that will be created on the subscription tables. The sp_script_synctran_commands procedure accepts two parameters: publication name and article name. For example, to script triggers for all articles, you can execute the following:

EXEC sp_script_synctran_commands 
@publication = ’publicationName’, @article = ’all’
  • Share ThisShare This
  • Your Account

Discussions

Make a New Comment

You must log in in order to post a comment.

Related Resources

There are currently no related podcasts. Please check back later.

Great password information at a small price
By John Traenkenschuh on June 13, 2009 No Comments

Where can cash-strapped security pro's get great information on security basics??

Steven HainesOracle Buys Sun of $7.4B
By Steven Haines on April 20, 2009 No Comments

In a stunning turn of events, Oracle steps in and buys Sun amist the breakdown of IBM's attempt to acquire Sun.

Buck WoodyIf it's Free it's for Me
By Buck Woody on January 26, 2009 No Comments

Sign me up for anything free these days. I just ran across a book that promises to help you build a web site for free...

See All Related Blogs

Informit Network