Doing a bulk insert in SQL Server 2012

I am trying to do a bulk insert from a csv file in SQL Sercer 2012:

BULK
INSERT Alias
FROM 'E:\filepath.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO

The problem is, the csv is missing a column that the table has. The table has Alias_ID that is supposed to auto increment by one every time a row is inserted. The CSV does not have this column so I get errors when I try to do this bulk insert. How can I handle a bulk insert where the columns do not match? I did try to edit the csv file so that there were blank values for the first column which I created called AliasID and just leaving a blank and a comma to signify no data but I guess that would be invalid since an ID column should not be null. Thanks in advance.
LVL 1
DB-ahaAsked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
in the case the file and the table don't match you need to use a format file:
http://msdn.microsoft.com/en-us/library/ms178129.aspx
0
 
DB-ahaAuthor Commented:
angelIII, in looking at the link you provided, it seems I have to use something called a bcp utility to first create the formatted file, then include the WTH FORMAT FILE reference in my code. The problem is, I have on idea what bcp is, how to use it. Could you break down the work process into something that is step by step to what I have to do/ Books online hasn't always been as informative as it could be.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
bcp is a command line tool to simplify the format file creation, but you can create the format file by hand also (see Sample Format Files in the page).
you basically create the format file in a folder on the sql server box, and refer to it WITH FORMAT FILE indeed.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
DB-ahaAuthor Commented:
Ok, dealing with the original problem, I modified the input file to have a column heading of RecordID and left all of the column blank to allow for the field names in both the input file and the destination table to be the same. The table's recordID column is the identity column and is set up to increment for every row inserted. When I do the bulk import now, all of the information imports and the record ID populates itself with the increment for the identity key but I get the error when doing the bulk insert:

The query completed with errors:

Msg 4864, Level 16, State 1, Line 8
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 1 (Record_ID).


However the rows all insert. What can I do to eliminate this error being generated since the input file is a csv file. and the record ID is set as INT to increment by 1 staring with 1?
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
for identity fields, it should be skipped, again via the format file.
if the csv file contains the id value to apply, you need to specify that you want to apply the IDENTITY_INSERT on.
0
 
DB-ahaAuthor Commented:
Please walk me through in simple terms using a list of instructions of how I can use the format tool.
0
 
DB-ahaAuthor Commented:
I haven't had time to try this, but this is a viable solution from my understanding of what this is.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.