简体   繁体   中英

SQL Server 2005 - Bulk Insert Problems

I am getting the following error message from a bulk load in sql server 2005, and was after ideas I can try to solve this problem.

Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 2, column 4 (type).

BULK INSERT dbo.BobTable FROM 'C:\DataFiles\Bob.txt' 
WITH (FIELDTERMINATOR = '","', FIRSTROW=2,ROWTERMINATOR = '\n',DATAFILETYPE='char')

Any ideas?

"id","altid","altid2","type"    
123456789.00,"ABC1234","ABC1234","R"

I cannot change the source file, but I can change the table that it is being inserted into. (Don't ask me why, but the decimals have been inserted at the end of an ID...I just have to deal with it.)

CREATE TABLE [dbo].[BOB](
  [id] [nvarchar](50) NULL,
  [Altid] [nvarchar](50) NULL,
  [Altid2] [nvarchar](50) NULL,
  [type] [nvarchar](50) NULL
)

Bulk Insert isn't good at handling Quote and Comma delimiters without help from a format file. You would need to create or generate one and then reference that with your Bulk Insert.

In your example, the first field ends with a ," , not "," so SQL thinks the field is too long.

Here's a good starting point: Creating a Format File

And this one on the application of the format file, which also has an example of your issue: Using a Format File to Bulk Import Data

I've used these methods before and it can be challenging maintain if your file format changes over time, but it gives you the necessary flexibility with the BULK INSERT process.

Also, are you able to use SSIS for any of this work? SSIS can handle the quote and comma delimited files with MUCH less pain than a BCP Format file. If you have the option, I fully recommend it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM