Fixing Missing Rows error in transactional replication:
· Set up transactional replication
http://sqlserverreplicationtroubleshooting.blogspot.com/2011/04/setting-up-transactional-replication-in.html
· Once it is done.
o Delete one row of data from subscriber.
o Update the same row in publisher
Go to replication monitor to view the error:
Command attempted:
if @@trancount > 0 rollback tran
(Transaction sequence number: 0x00000015000001B9000400000000, Command ID: 1)
(Transaction sequence number: 0x00000015000001B9000400000000, Command ID: 1)
Same error you can view from following table
Select * from msrepl_errors
You can see that there are 2 items mentioned in error one is transaction sequence number: 0x00000015000001B9000400000000 and another is command id:1
System Procedure sp_browsereplcmds takes the values transaction sequence number and command id as parameters and outputs the command that caused the errors.
Transaction sequence number:
Identifier for the transaction which contains commands.
Command ID:
Each command have a unique ID within the scope of the transaction sequence. The command starts at 1 and increment by 1 for each new command. Command should be updated in same sequence if it is more than one command.
Now How to fix the error:
· Get the article id and publisher database id from following query on distributor database using,
select *
from msrepl_commands (nolock)
where command_id = 1 and xact_seqno = 0x00000015000001B9000400000000
· Use the output of above query in following statemet on distributor DB,
sp_browsereplcmds @article_id = 1, @command_id = 1 , @xact_seqno_start = '0x00000015000001B9000400000000', @xact_seqno_end = '0x00000015000001B9000400000000',@publisher_database_id = 1
· Copy the command column from above statement output,
{CALL [dbo].[sp_MSupd_dboTestTable] (,N'testDelete',2,0x02)}
check for the sp_helptext on the proc
Create sql statement as mentioned in Procedure
SQL Server creates 3 procedure on subscriber which starts with sp_MS
[sp_MSdel_dboTestTable]
[sp_MSins_dboTestTable]
[sp_MSupd_dboTestTable]
· You can see from above statement that for ID – 2 update statement is failed
· Go to subscriber and insert the row for ID – 2
INSERT INTO [xyz].[dbo].[TestTable]
([id]
,[text])
VALUES
(2
,'Anydata')
GO
· Go and verify the replication monitor error is gone.
· Get the MaxID of the Article on the subscriber from below :
Get the Range of missing Rows:
By connecting to the subscriber Get the count
select COUNT(*) from TestTable(nolock) where id between 4 and 6 --1
Get the Missing count by doing
select (Get the count from publihser from above)- (get the count from subscriber from above)=
Inserting the missing rows manually into subscriber:
SELECT A.* FROM TestTable_ReplIssue__1 A (nolock)
Left Join TestTable (nolock)B On A.id = B.id
Where B.id Is null
COMMIT
If there is any problem with Linked server while inserting the records follow the steps below
select * from sys.servers
If there are mutiple rows are missing then follow the below steps:
· Get the MaxID of the Article on the subscriber from below :
select MAX(id) from TestTable
Get the Range of missing Rows:
By connecting to the subscriber Get the count
Get the missing ID from the procedure call ,Missing ID (in subscriber) and the MaxID if the article(in subscriber), Get the count
select COUNT(*) from TestTable(nolock) where id between 4 and 6 --1
By connecting to the publisher Get the count
select COUNT(*) from TestTable(nolock) where id between 4 and 6 --3
Get the Missing count by doing
select (Get the count from publihser from above)- (get the count from subscriber from above)=
Select 3 - 1 2 (Missing Rows Count)
Inserting the missing rows manually into subscriber:
· Create a temp table in the subscriber with same structure as the article(table being replicated)
select * INTO TestTable_ReplIssue__1 FROM TestTable (nolock) where 1=0
· Insert the rows into the temporary table by selecting the data from publisher using the linked server.
INSERT INTO TestTable_ReplIssue__1
select * FROM [MININT-8JHDRAI].TestDB.dbo.TestTable
where ID between 4 and 6
· Insert the missing rows into the article by comparing with the temp table created above.
BEGIN TRAN
INSERT INTO TestTableSELECT A.* FROM TestTable_ReplIssue__1 A (nolock)
Left Join TestTable (nolock)B On A.id = B.id
Where B.id Is null
COMMIT
If there is any problem with Linked server while inserting the records follow the steps below
select * from sys.servers
sp_dropserver 'MININT-8JHDRAI', 'droplogins';
EXEC sp_addlinkedserver
'MININT-8JHDRAI',
N'SQL Server'
GO
Hi - have you seen any situations where you get no results?
ReplyDeleteselect
*
from
msrepl_commands (nolock)
where
command_id = 1
and
xact_seqno = '0x0001ED42000001B8000100000000'
No, I haven't seen the situation where you get no results. See whether you are querying on right publisher DB.
DeleteThis comment has been removed by the author.
ReplyDeleteWhat happened if its trying to delete records from subscriber which is not exists in subscriber ?
ReplyDeletefor example
{CALL [dbo].[sp_MSdel_dboTestTable] (2}
So, the id is no exists in both database. Is there better way if there are multi rows missing?
ReplyDelete
I would like to find rows missing in all the tables in database.in transacion replication
ReplyDeletePlease can any one send query and process for this