Consider the following scenario. You configure a transactional replication in Microsoft SQL Server 2005. You create the initial snapshot successfully on the publisher. In this scenario, when the Distribution Agent tries to apply the snapshot to the subscriber, you receive the following error message:
Error: 137, Severity: 15, State: 2 Must declare the scalar variable "@Variable".
This issue occurs if the following conditions are true:
The transactional replication contains a created article that is based on a table that has computed columns.
The primary key column is sorted after the computed columns are sorted.
From the syscolumns system table, you can see that the column ID of the primary key column is larger than column IDs of the computed columns.
The publication uses concurrent snapshot processing.
The Distribution Agent creates a stored procedure to insert data on the subscriber. However, the stored procedure has incorrect parameter numbers.
Cumulative update information
The fix for this issue was first released in Cumulative Update 2. For more information about how to obtain this cumulative update package for SQL Server 2005 Service Pack 2, click the following article number to view the article in the Microsoft Knowledge Base:
936305 Cumulative update package 2 for SQL Server 2005 Service Pack 2
Note Because the builds are cumulative, each new fix release contains all the hotfixes and all the security fixes that were included with the previous SQL Server 2005 fix release. Microsoft recommends that you consider applying the most recent fix release that contains this hotfix. For more information, click the following article number to view the article in the Microsoft Knowledge Base:
937137 The SQL Server 2005 builds that were released after SQL Server 2005 Service Pack 2 was released
Microsoft SQL Server 2005 hotfixes are created for specific SQL Server service packs. You must apply a SQL Server 2005 Service Pack 2 hotfix to an installation of SQL Server 2005 Service Pack 2. By default, any hotfix that is provided in a SQL Server service pack is included in the next SQL Server service pack.
To work around this issue, use one of the following methods:
When you create the underlying table of the article, make sure that the primary key column is sorted before the computed columns are sorted.
Use native snapshot processing or database snapshot processing instead of concurrent snapshot processing.
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.
For example, this issue occurs when you use the following statement to create the underlying table of the article:
CREATE TABLE [dbo].[test]( [col1] [char](10) NOT NULL, [col2] [char](10) NOT NULL, [col_1_2] AS ([col1] + [col2]), [ID] [int] IDENTITY(1,1) NOT FOR REPLICATION PRIMARY KEY NOT NULL)