Quantcast
Channel: SQL Server Replication forum
Viewing all articles
Browse latest Browse all 4054

Replication Problem in Stored Procedure with identity column insert

$
0
0
Hello,

I have created two Publications
a) [TestDB]:Pub_ident  (contains an article "ident" table containing identity column)
b) [TestDB]:Pub_Stp    (contains an article "stp_ident" procedure which inserts data in ident table)

After creating subscriber of [TestDB]:Pub_ident publication, a table ident is present in Subscriber DB,
it's identity column is marked as Not For Replication.

After creating subscriber of [TestDB]:Pub_Stp, a procedure is present in Subscriber DB,
code of both TestDB & Subscriber DB is same.


After, alter procedure stp_ident, 
I am getting Error:

Column name or number of supplied values does not match table definition. (Source: MSSQLServer, Error number: 213)
Get help: http://help/213


After lots of googling, I have got the reason behind is insert into ident table,
containing identity column is marked Not for replication.

--------------------
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/b992c452-0a20-4a36-a51b-453730b49248/replication-problem-with-stored-procedure?forum=sqlreplicationhttps://social.msdn.microsoft.com/Forums/sqlserver/en-US/b992c452-0a20-4a36-a51b-453730b49248/replication-problem-with-stored-procedure?forum=sqlreplication

factors:

1) Replicated identity columns are marked "not for replication" by default at the publisher when you setup replication through SSMS in SQL2005

2) Any insert statements into a table with an identity column marked 'not for replication' executed from a replication agent 
(distribution agent for example) must supply an explicit value for the identity column

3) In SQL2005, the server verifies the validity of insert statements in stored procedures and triggers during the creation of these 
objects and raises error if it determines that an embedded insert statement does not supply all the required values. 


In more concrete terms, 1) will basically lead to all identity columns being marked 'not for replication' at both the publisher and the subscriber 
and given that your replicated stored procedure is going to be created through a distribution agent connection, 2) and 3) will lead the subscriber 
server into thinking that the insert statement embedded in your stored procedure would need to supply an explicit value for the identity column 
marked 'not for replication' and so it raises an error when an explicit identity value is found missing. Now, the stored procedure that you are 
replicating is probably intended to be executed outside of replication even though it is created through replication so the check is technically 
invalid despite the good intentions behind it.

------------

as a workaround I had disabled 'not for replication' property on ident table's the identity column.


After this replication started again, I had made changes to procedure stp_ident and those changes were reflected,

now after sometime I had enabled 'not for replication' property on ident table's the identity column.

To my surprise replication was working fine,

I want to know what is the reason behind this?
Is there a chance for same error to come again, as I am using Replication on Production server.

Viewing all articles
Browse latest Browse all 4054

Trending Articles