?
Solved

Doing a bulk insert in SQL Server 2012

Posted on 2013-01-07
7
Medium Priority
?
723 Views
Last Modified: 2013-05-30
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.
0
Comment
Question by:DB-aha
  • 4
  • 3
7 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 38752676
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
 
LVL 1

Author Comment

by:DB-aha
ID: 38752866
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38753596
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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 1

Author Comment

by:DB-aha
ID: 38759800
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38763904
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
 
LVL 1

Author Comment

by:DB-aha
ID: 38788892
Please walk me through in simple terms using a list of instructions of how I can use the format tool.
0
 
LVL 1

Author Closing Comment

by:DB-aha
ID: 39207689
I haven't had time to try this, but this is a viable solution from my understanding of what this is.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question