Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 bulk insert does not allow primary key

Author  Topic 

mary_itohan
Posting Yak Master

191 Posts

Posted - 2014-04-25 : 09:11:49
Hello guys,
I am using the bulk insert statement below to import data from a csv and excel files. As I am running 64bit versions of windows, and could not find any sql import statements.

However I noticed it does not allow any primary key, hence after importing to the temp table, I then import in sql to the main table.

Is there anyway to fix the temp table ? ie add a PK value

thanks

M

BULK INSERT CSV_TESTING
FROM 'd:\MAM-NAP.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
);




_____________________


Yes O !

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2014-04-25 : 10:11:12
No problem bulk inserting to a table with a PK.

>>However I noticed it does not allow any primary key
Not sure what you mean by this. If you put a primary key on the target table or not Bulk Insert will insert to it. Of course your data can't violate that constraint otherwise the operation will fail. So in that regard I think it is good practice to bulk insert to a staging table anyway.

Be One with the Optimizer
TG
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-04-25 : 11:33:48
Also: if your PK column is an identity column, and you want to import the values of that column into the table, you will need to specify the KEEPIDENTITY option in BULK INSERT. http://technet.microsoft.com/en-us/library/ms188365.aspx
Go to Top of Page
   

- Advertisement -