Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Doing a bulk insert in SQL Server 2012

Posted on 2013-01-07
7
Medium Priority
?
716 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

647 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