Solved

Doing a bulk insert in SQL Server 2012

Posted on 2013-01-07
7
678 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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
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
Comment Utility
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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:DB-aha
Comment Utility
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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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
Comment Utility
I haven't had time to try this, but this is a viable solution from my understanding of what this is.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now