@@IDENTITY system variable

Microsoft SQL Server has a system variable named @@IDENTITY, which stores the most recent value of the identity column after an insert operation. When a Replication Agent trigger executes to capture an insert operation in a marked table, that trigger performs an insert operation in one of the transaction log shadow tables. As a result of the trigger execution, the value of the @@IDENTITY variable after an insert operation in a marked table is actually the value after the trigger’s insert in the shadow table. Since the Replication Agent shadow table does not have an identity column, the value of the @@IDENTITY variable is set to NULL.

If you have a SQL Server application that uses the @@IDENTITY variable, use the following procedure to work around this problem:

StepsTo work around the @@IDENTITY variable problem

  1. Create a table in the primary database that has a single column defined as integer identity.

    This column will be used to store the original value of the @@IDENTITY variable.

  2. Modify the Replication Agent insert trigger (prefixinstrg_xxx) as follows:

    After the original value of the @@IDENTITY variable is inserted in the identity column in the new table, the value of the @@IDENTITY variable is restored.