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_Accounts 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.
Related
Not able to perform Upsert in SQL Server Management Studio(SSMS) as the data contains Identity column Sequence number & Rowid.
I am trying to perform incremental load from DB2 to SSMS. In the sink side I am selecting Upsert but throwing the error as:
Can not update identity column
I should not use dataflows to achieve this scenario.
Can anyone suggest how to do this for many tables dynamically to set the identity flag to
Yes if there is insert else I need to set identity flag as No?
Thanks in advance
I tried with set identity_insert dbo.test on to insert manually for one table
but need help to set up this dynamically.
ErrorCode=SqlOperationFailed,'Type=Microsoft.DataTransfer.Common.Shared.HybridDeliveryException,Message=A database operation failed. Please search error to get more details.,Source=Microsoft.DataTransfer.ClientLibrary,
''Type=System.Data.SqlClient.SqlException,Message=Cannot update identity column 'SEQNR'.,Source=.Net SqlClient Data Provider,SqlErrorNumber=8102,Class=16
The Database.Execute DAO method has so many parameters and I'm not sure what would be the optimal way (fast, efficient and avoiding locks) to execute update and insert statements againts an MSSQL database from VBA code (MS Access). I'm accessing the MSSQL tables through ODBC.
Database.Execute method (DAO)
Now I'm using it as follows:
CurrentDb.Execute "SQL statement comes here"
or
CurrentDb.Execute "SQL statement comes here", dbSeeChanges
I have two scenarios:
I want to update or insert data into business tables (customers, invoices etc.) - here I use the version with dbSeeChanges - because multiple users may read and write these tables at the same time
I insert records into a log table - I use the plain version here - only inserts happen in this case, there's no read from these tables
But I'm not sure if this is really the best way to use it.
what you use now is fine...have used myself in very large apps with SQL Server in the back end....
I have an Access 2003 database that data is inserted into throughout the day and I want to convert to a SQL Server database. There are many different instruments and processes that insert into the Access database, so it would take a lot of modification to change all of them to insert into SQL Server.
I know Microsoft has a tool to convert an Access database to a SQL Server one, but as far as I can tell, it can't be run automatically and it wouldn't be able to just import the new data since the last "merge" (please correct me if I am wrong)
What would be the best way to go about this problem? Maybe some sort of script?
Thanks.
There are only inserts on the Access side, and the SQL will be read-only
Then I would do this:
Link the SQL Server tables into the Access database
Run INSERT queries to copy the new data into the server tables. This assumes that all tables have primary keys.
The queries would e.g. look like this (air code):
INSERT INTO dbo_Customers
SELECT src.*
FROM Customers AS src LEFT JOIN dbo_Customers AS tgt
ON src.ID = tgt.ID
WHERE tgt.ID IS NULL
Customers is the local table, dbo_Customers the server table.
The query selects all records from Customers that don't exist in dbo_Customers.
Since several other processes access the original database, it's probably best to set up a new Access database, link both the original and the Server tables, and run the INSERT queries in the new db. You don't want any code running in the "data" db.
It can be automated e.g. with a Form_Timer procedure.
I need to create a trigger on one server to fire when a certain table is changed, after which I have to do some calculation with the data so I can import (it's no just copying) it into another table on a different server.
Also I only want to import the new data that hasn't been imported through a earlier trigger.
How would I go about this?
Create Linked Server to your target server, then use MERGE statement to perform action depending on existence of record in destination table.
I would refrain from creating triggers referring remote servers though, it might have performance and reliability implications. Consider using Service Broker if you want to achieve small latency and still make the update reliable.
You can create a linked server between two SQL Servers to send data from one to the other.
To create a linked server you need to use the system stored procedure sp_addlinkedserver, you can also do it through SQL Server Management Studio I believe.
Here is an example you can try:
EXECUTE sp_addlinkedserver #server=N'serverip/hostname', #provider=N'SQLNCLI'
You can view if your linked server has been created by querying sys.servers
You can query the linked server database with the following syntax:
SELECT x
FROM [linkedservername].[database].[schema].[table]
More information: http://msdn.microsoft.com/en-us/library/ff772782.aspx
For the trigger updating only data which hasn't been handled before. There are many ways you can do this. If your source table has an 'update date' type column, you can do it based on that. Alternatively, if your table has an identity column and you want to copy data incrementally you can store in a table the 'last id' which is copied over each time, then on the next run of the trigger you can tell it to start from that id+1 that way rows are only transferred once.
I am getting "1" as value of IDENT_CURRENT('TableName') for replicated article table in subscriber database in sql server. Actually it should be same as table in publisher database.
Any solution and setting to back it correct figure?
This could well be down to design in so far as you are not directly inserting the identity when replicating. The actual mechanism in use may be at a lower level and should you actually want to insert directly into the subscriber table, you may find it works as it should (I am not recommending you try this however!)
You could try checking/updating the value an re-running your query
DBCC CHECKIDENT (Transact-SQL)