SQL Server 2016 CSV file import - sql-server

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.

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.

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

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.

How to import Case-Sensitive data from Oracle to SQL Server using SSIS

I am trying to import data from Oracle into SQL Server using SSIS.
The problem is I have a PK of datatype VARCHAR2(200) in one of the tables having case-sensitive data in Oracle DB. Hence, the SSIS, while importing the data, is throwing
Violation of PK, cannot insert duplicate value in PK
How should I work around this? Any solution for this EXCEPT accepted answer of this because it's not feasible for me to drop and create the DB for enabling case-sensitive data?
You don't need to Recreate database. You just need to set Case Sensitive column.
Open in Design mode Table, choose your column and push Collation row.
Just check "Case Sensitive" checkbox, push OK and Save Table. now It will be OK.
If you can add a new column, set its collation to case sensitive one, reload the records and then rename them accordingly:
SELECT 1 AS TEST INTO #TT
ALTER TABLE #TT ADD new_pk_case_sensitive VARCHAR(200) COLLATE Latin1_General_CS_AS

How do I generate a primary key using SSIS?

I'm mucking about with SSIS and can't figure out how to generate a primary key?
I have a very simple SSIS Data Flow:
Excel File Source -> Character Map -> ADO.NET Destination
The Excel file has the following structure:
Field1
The destination table has the following structure:
ID - UniqueIdentifier (GUID)
Field1
There's no problem mapping Field1, but how do I get ID to map to the NEWID() SQL Server function (or something equivalent like the .NET method System.Guid.NewGuid()).
What you do is create a DEFAULT CONSTRAINT for the ID field as NEWID(). In SQL Server, go to the table design, click on the ID column and in the properties, look for DEFAULT. There, type NEWID(). Save the table.
In SSIS, only insert into Field1, and the ID will automatically be generated at every attempted insert.

Resources