SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 bulk insert does not allow primary key
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mary_itohan
Posting Yak Master

191 Posts

Posted - 04/25/2014 :  09:11:49  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
6062 Posts

Posted - 04/25/2014 :  10:11:12  Show Profile  Reply with Quote
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

Edited by - TG on 04/25/2014 10:12:48
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3575 Posts

Posted - 04/25/2014 :  11:33:48  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000