2013-04-11 146 views
2

我試圖在SQL Server 2012中使用BULK INSERT將數據從文本文件導入到數據庫表中。我正在使用文本文件中的Z指示符(DOB字段)的UTC日期時間值。但在執行BULK INSERT時,我收到「數據轉換錯誤」。批量插入日期時間錯誤

任何人都可以幫我嗎?

腳本

CREATE TABLE Info 
(
FirstName VARCHAR(40), 
LastName VARCHAR(40), 
DOB DATETIME 
) 

BULK INSERT Info FROM 'F:\InfoTextFile.txt' WITH (FORMATFILE='F:\InfoFormatFile.fmt') 

的F含量:\ InfoTextFile.txt的

John Smith 1988-08-23T00:00:00.0Z

William Alexander 1987-07-22T00:00:00.0Z

內容 'F:\ InfoFormatFile.fmt'

11.0 

3 

1  SQLCHAR  0  10  ""  1  FirstName  "" 

2  SQLCHAR  0  10  ""  2  LastName  "" 

3  SQLDATETIME 0 22  "\r\n" 3  DOB   "" 

錯誤:

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

Msg 4832, Level 16, State 1, Line 2 Bulk load: An unexpected end of file was encountered in the data file.

Msg 7399, Level 16, State 1, Line 2 The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error.

Msg 7330, Level 16, State 2, Line 2 Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".

回答

2

嘗試這一個 -

--C:\InfoTextFile.txt -> 
--John Smith 1988-08-23T00:00:00.0Z 
--William Alexander 1987-07-22T00:00:00.0Z 

--C:\InfoFormatFile.fmt -> 
--11.0 
--3 
--1  SQLCHAR  0  10  " "  1  FirstName  "" 
--2  SQLCHAR  0  10  " "  2  LastName  "" 
--3  SQLCHAR  0  22  "\n" 3  DOB   "" 

BULK INSERT Info 
FROM 'C:\InfoTextFile.txt' 
WITH (FIELDTERMINATOR = ' ', ROWTERMINATOR = '\n', FORMATFILE='C:\InfoFormatFile.fmt')