How to import to a SQL Server table and start primary key where left off - sql-server

I can't seem to find an answer to this. I am very new to SQL Server. I have been trying to set up a database to be updated daily for a website.
There is a .CSV file produced daily. I have set up a script to copy the file, edit the text and import the file into a table in SQL Server 2012.
There are 16 fields in the .CSV file. I have a 17th field in the table I import it into.
The 17th field is the Primary Key which I have set to autoincrement.
My problem is this:
I'm implementing this as a new process. This is already set up and in operation on an older server. The older server was using MySql. The Primary Key was left off at 81,720,024.
I have set the Primary Key field to autoincrement with a seed of 81720024.
Every time I update the table I truncate the table first and the import from a staging table. The Primary Key always starts at 81720024. I need to have it increment from the last entry it had. Please help!

Try deleting from the table instead of truncating.

Related

How can I import Excel data into an existing table in SQL Server and keep primary key incrementing accordingly

I am using the SQL Server Import and Export wizard to import data from an Excel sheet into an existing table in my database. I want to append the data to the existing data, however I am stuck on figuring out how to keep the primary key in this table incrementing with the data I import. I don't have the [ID] column in my Excel sheet. The last value in my [ID] column is 105, so I would like for it to continue from there once I import.
Is there any way to do this through the wizard? I've tried the "Enable Identity Insert" check box in the column mappings window, however I still receive an error that I can not insert null values
On which column is the NULL error? If it's the ID column, you should just ignore it as long as it's an identity column when the rest of the data is imported it should automatically increment. If not you can undo and CHECKIDENT to change the ID it'll start at.
It should be simple. Alter your ID column as identity column increment by 1 and while importing data from your excel, don't map or do anything to this column. Just import all other columns without Enabling Identity Insert check box

How to create identity column when importing data from Excel into MS SQL Server (with Import and Export Wizard)?

I need to import great amount of data from excel into MS SQL Server, using the Import/ Export Wizard. Then I'll continue importing more data into the same table on a weekly basis.
The bad thing is that my excel data doesn't have an identity column, which to use as a primary key. The only option with what available is to use 2 string columns as a primary key, which is not a good idea.
Is there a way the sql server to add auto-identity column (integer) when importing the data, and what's the trick? I prefer such a column to be automatically added, because I'll need to import big amount of data into the same table on a weekly basis.
I tested a couple of times (with no success) and looked for a solution in the internet, but didn't found an answer to that particular question. Thanks in advance!
You can create the table first along with the new identity column.
CREATE TABLE YourTable
(
id INT IDENTITY,
col1....
col2....
col3....
PRIMARY KEY(id)
)
Then run the import/export wizard. When you get to the destination section pick your newly created table and map all the fields except the identity column. After the import you can check the table and see the id column has been populated.
Column names in Excel sheet should be same as that of sql Table.
Map Excel columns with that of SQL table columns by Clicking Edit Mapping.
Just don't map that (identity) column of sql table to anything .
In Import-Export Wizard don't check Enable identity insert Checkbox. (Leave that
un selected).
and go ahead import .. This worked for me. !!!!!
Previously when i used to check Enable identity insert it used to give me error.
I had a similar issue. I have a SQL table with an identity column (auto increment ID value) defined. I needed to import an Excel spreadsheet into this table.
I finally got it to work via the following:
Do NOT add a column to the Excel spreadsheet for your identity column in the SQL table.
When you run the import wizard and get to the Edit Mappings step, do NOT select the Enable identity insert checkbox. This, in particular, was tripping me up.

SQL Server 2016 CSV file import

I have a table dump in CSV format. Using SQL Server Management Studio I can import that CSV file, but the problem is I can not set a column as primary key.
My CSV file/ table has column and I want to set PARETNT_NAME as primary while importing.
PARENT_NAME | QUANTITY | COMPONENT_NAME
Please guide me how to do this.
Thanks in advance
During the import there is a step where you pick the table to import to. As part of that you can manually define the SQL CREATE TABLE statement. I would assume you could modify that to specify the primary key. But I haven't ever tried to do this.
However, unless you have a specific reason to do this before the import, I would just use an ALTER TABLE statement to modify the column to be not NULL and set it as the primary key. To me that just seems easier.

Copy a table and dependencies

I made an error in updating a table and the quickest solution was to replace the table with a backup from previous night. No other changes had been made to the table prior to the backup.
I renamed the existing table that had errors to table_old and used SQL import tool to import the backup copy of table into the correct database. I realize that my dependencies are not present for the copied table.
Q: is is possible to copy the dependencies into the table that I used for the backup? I assume the dependencies would be included if I imported data + Schema?
Regards,
I would try this:
Rename the new table to table_backup. Rename original table to its original name table. Truncate table table. Insert table Select * from table_backup. You may need to set identity insert on if you have such a column in the table.

Incorrect Duplicate insert problem with SQL Server CE 3.5

I am not able to insert data into my table anymore!
Here's my table design.
intId is the Primary Key, there's no explicit unique constraint defined on it, has identity increment set to 1 and identity seed to 1.
I am inserting data into this table thru LINQ.
testDB.tbl_Vehicle.InsertOnSubmit(newVehicle);
testDB.SubmitChanges();
All this used to work till now, and all of a sudden it stopped working!
It now says
A duplicate value cannot be inserted into a unique index. [ Table name = tbl_Vehicle,Constraint name = PK_tbl_Vehicle ]
More info: This desktop application has 1 executable and 1 .sdf file. It was developed on Win 7 and recently was moved to Win XP system. But that shouldn't be a problem as there are other tables I am inserting into with similar logic and table design.
Do one thin make use of SQL profiler and check the query fire on insert statement.
More on check the database Table again and if possbile set the seed for the primary i.e identity column.

Resources