开发者

Pass-through queries and linked table queries using different connections

Follow-on to an earlier question: Force SET IDENTITY_INSERT to take effect faster from MS Access

I'm in the process of upsizing from an MS Access backend to a SQL Server backend. Many of the tables have autonumber primary keys. I'm trying to populate them as follows (pseudocode):

ExecutePassThru "SET IDENTITY_INSERT dbo.Accounts ON"
db.Execute "INSERT INTO sql_Accounts SELECT * FROM mdb_Accounts"
ExecutePassThru "SET IDENTITY_INSERT dbo.Accounts OFF"

In the above code, sql_Accounts is a linked table connected to the Accounts table in SQL Server and mdb_Accoun开发者_开发技巧ts is a linked table connected to the Accounts table in the mdb. This fails with the error,

Cannot insert explicit value for identity column in table 'Accounts' when IDENTITY_INSERT is set to OFF.

I fired up SQL Server Profiler to try to figure out the problem and the issue is that each line of code in my sample may or may not use a different Server Process ID (SPID). In other words, they are using different connections to the backend.

Is there some way to ensure they all use the same connection?


There may be a more elegant solution, however this a one time conversion and it's not worth too deep of dive trying to do complex things with connection reuse.

  1. Create a staging table on the SQL Server that does not contain an identity but is otherwise of the same schema.
  2. Load your data to that table
  3. Do your identity insert entirely on the SQL Server side.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜