How to handle Identity column in replication

  • I am facing a problem while replicating my database. My replication is working fine but I could not use the replicated database becasue all the identity columns in the source table are not identity columns in the target database. Can anybody help me on this

  • replication needs an identity column create new identity column in both servers.

  • First a correction to the previous post. Replication does not require a table to have an identity column. For transactional replication, all that is required is a declared primary key. It is up to you to deal with how new primary key values are created. One option is to identity but you can choose any method you like. In fact, the primary key does not even need to be numeric.

    If you are using an identity column for the primary key, you should also declare it as "NOT FOR REPLICATION". This tells replication that when it inserts a record into the subscriber, do not generate a brand new identity value (similar to using SET INDENTITY_INSERT ON/OFF).

    It sounds like there is update activity on the subscriber (I am assuming that you are using transactional replication with updateable subscribers). You will need to manage identity ranges if this is happening (well, actually only if you are inserting on the subscriber). The replication sproc sp_Addarticle allows for this with parameters @auto_identity_range , @pub_identity_range , @identity_range and @threshold. If you are using the new publication wizard, these are available from the article properties in the section "Identity Range Management".

    If you are using Merge Replication, I probably would not bother with identity columns at all since Merge Replication requires you to have a row guid column. If you were to have an identity column as well, you would effectively have two completely separate unique columns on the table which is a bit of a waste.

  • Very informative!

    Follow up question, If column ID defined Identity not for replication.

    How does SQL Server tells the difference between a user calling a store proc to insert or a batch insert executed by replication, restore by log shipping, bcp, or your own batch insert script?

    Also, upon any insert, is the already generated column ID stored in all the log shipping/backup log files?

    Many Thanks,

    Jeffrey

  • To tell you the truth, I have never really bothered to discover how SQL Server works out that it is the distribution agent that is inserting data. I assume that the agent is calling system stored procs to do its stuff. If you really need to know, SQL Profiler will most likely give the info.

    As with any other INSERT/UPDATE/DELETE, all replication changes to the database are recorded in the transaction log.

  • Thank you, you saved my day 🙂

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply