Enable identity insert is not working when importing data - sql-server

I am trying to import many tables from access db to MS SQL server using the import wizard.
Some rows in source tables has been deleted so the sequence of IDs are like this: 2,3,5,8,9,12,...
but when I import the data into my destination, the IDs start from 1 and increment by 1, so they don't exactly match with source data.
I even check the "Enable Identity insert" but it does not help.
The only work around I have found is to change the IDs in destination tables from Identity to integer one by one, then import, and then change them back to identity, which is very time consuming.
Is there any better way to do this?

If you want to insert an id in the identity column, you need to use:
SET IDENTITY_INSERT table_name ON
https://msdn.microsoft.com/es-us/library/ms188059.aspx
Remember to set it OFF at the end of the script.

Related

SQL Server BACPAC Import fails with IDENTITY_INSERT error

I'm importing a bacpac from Azure to a local SQL DB. The process goes for a while and on about the 50th table it fails with this error:
IDENTITY_INSERT is already on for table 'X'.
Cannot perform set operation for table 'Y'
Table 'X' was successfully processed already (it was like table #45 in the list).
Table 'Y' is the one currently processing (like table #50 in the list).
After the error/failure I see rows on table Y so it seems at one point IDENTITY_INSERT was ON for that table. Not sure what else to check.
As a workaround use the Import/Export Wizard instead. After selecting the source and destination tables, click on edit mappings. In the resultant pop-up, click on edit SQL and edit the auto-generated SQL and add IDENTITY (1,1) at the end of the column you want to set IDENTITY property. Ensure that you have enabled IDENTITY INSERT ON by checking the relevant box.

Append one ore more rows from Excel to existing SQL Server table

I have imported an Excel table into SQL Server 2016 Express and made a table using the import wizard.
Now when I update my Excel sheet, e.g add one ore more row to it, I also want to update my SQL Server table, that means, adding the one ore more rows to the table as well. What's the easiest way to do this? In an "append" rows manner. I don't want to add the whole Excel sheet again..
You asked for the easiest solution. Since you are already comfortable using the wizard it would seem to me that the easiest way is to import the "updated" Excel sheet / file into SQL Server Express using the wizard as well. Yet, import it into a new table (without removing the old one).
Afterwards, insert new rows or update the existing records on the SQL Server with a simple SQL MERGE statement. Afterwards, you can drop / delete the imported table again (because the existing table has been updated).
While I do not know your tables the following SQL code sample shows a simple merge on a basic customer table where tblCustomers would be the exiting table (to be updated / insert new rows) and tblCustomersNEW would be the new import (which will be deleted again once the update / append is complete):
merge dbo.tblCustomers as target
using dbo.tblCustomersNEW as source
on source.ID = target.ID
when matched then
update set target.Name = source.Name,
target.Country = source.Country
when not matched by target then
insert (Name, Country)
values (
source.Name,
source.Country
);
Note, that the MERGE statement requires a semicolon at the end similar to CTE requiring a semicolon before you start ; With myTable as....
For more information on the MERGE statement you might want to read the following on article on MSDN: https://msdn.microsoft.com/en-us/library/bb510625.aspx

ArcGIS SQL Server ArcSDE: Versioning vs Identity Column

[I am asking here instead of GIS Stackexchange because this maybe more of a SQL Server issue?]
I have SQL Server ArcSDE connection in which data is batch inserted via some scripts. Currently, anytime there is a new row of data then an 'OBJECTID' column, set to INT and Identity Column increases by number 1. So far so good. Except I need to enable "versioning" on the table.
And so I follow this: http://resources.arcgis.com/en/help/main/10.1/index.html#//003n000000v3000000
but get errors because ArcGIS is complaining about the Identity column, per: http://support.esri.com/cn/knowledgebase/techarticles/detail/40329 ; and when I remove the Identity attribute to the column then the column value becomes NULL--not good.
So, in my scenario, how I can I increase the value of OBJECTID by 1 number as auto-increment? I supposed, I can just insert some GUID into the 'OBJECTID' field through the script? Also, if I follow the GUID route then I am not sure if I will be able to add rows manually via ArcGIS Desktop on occasional basis?
Thanks!
Update 1 Okay, so changed the OBJECTID field to a 'uniqueidentifier' one with a default GUID value and now I am able to enable "versioning" using ArcGIS Desktop. However, ArcGIS is expecting GUID to be an INT data type--and so no go?
In light of my "update 1" in the Question above I managed to take care of this by inserting an INT value for OBJECTID during the batch insertions per the following: How to insert an auto_increment key into SQL Server table
so per the above link, I ended doing:
INSERT INTO bo.TABLE (primary_key, field1, fiels2) VALUES ((SELECT ISNULL(MAX(id) + 1, 0) FROM bo.Table), value1, value2)
EXCEPT in my case the IDENTITY remains not 'ON' at all either in the database or, unlike the above link, I didn't have to set Identity On/Off during batch insertions; Works for some reasons anyway!

insert into table from another table if there is any duplicates do not insert

Ok I have a database with a table LOOKUP (1st), and the same database on another server also with LOOKUP (2nd).
Is there a way I can insert into the 1st database from the second, if duplicate exist then skip else all other values that is present in 2nd should be inserted into 1st. Basically I want the exact same Database!
The think that confuses me is they are on different servers.
Can I export the one to like excel and import it again and replace my database or anything.
You will have to use 2 MERGE queries if you want to make both the databases identical. This is because the first merge will only insert records that are available in DB1 into DB2. But, DB1 will still not contain the records present in DB2 but not in DB1.
I would suggest you to do this task using SSIS.
You can use 2 sources DB1 and DB2 and a LOOKUP transformation on each source (LKP1 and LKP2).
Then you can insert the No Match output of LKP1 into DB2 as destination and No Match output of LKP2 into DB1 as destination.
This will solve the multi-server issue as well because you can create connection to any server in SSIS.

How to merge table from access to SQL Express?

I have one table named "Staff" in access and also have this table(same name) in SQL 2008.
Both table have thousands of records. I want to merge records from the access table to sql table without affecting the existing records in sql. Normally, I just export using OCBC driver and that works fine if that table doesn't exist in sql server. Please advise. Thanks.
A simple append query from the local access table to the linked sql server table should work just fine in this case.
So, just drop in the first (from) table into the query builder. Then change the query type to append, and you are prompted for the append table name.
From that point on, just drop in the columns you want (do not drop in the PK column, as they need not be used nor transferred in this case).
You can also type in the sql directly in the query builder. Either way, you will wind up with something like:
INSERT INTO dbo_custsql
( ADMINID, Amount, Notes, Status )
SELECT ADMINID, Amount, Notes, Status
FROM custsql1;
This may help: http://www.red-gate.com/products/sql-development/sql-compare/
Or you could write a simple program to read from each data set and do the comparison, adding, updating, and deleting, etc.

Resources