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.
- Create a staging table on the SQL Server that does not contain an identity but is otherwise of the same schema.
- Load your data to that table
- Do your identity insert entirely on the SQL Server side.
精彩评论