使用格式文件

时间:2015-04-30 11:03:44

标签: sql-server file csv format bulkinsert

我正在尝试使用批量插入和格式文件将CSV文件插入SQL数据库,但我遇到了一些麻烦。我一直在使用以下教程。

http://www.bidn.com/blogs/marcoadf/bidn-blog/2479/bulk-insert-format-file-skip-column

当我运行脚本时,我收到此错误,我认为它与格式文件的格式有关。

Msg 4863, Level 16, State 1, Line 3
Bulk load data conversion error (truncation) for row 2, column 3 (Ref).
Msg 7399, Level 16, State 1, Line 3
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 3
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".

表格式

[Id]       INT           NULL,
[Name]     NVARCHAR (50) NULL,
[Townland] NVARCHAR (100) NULL,
[Ref]      INT           NULL,
[Lat]      FLOAT (53)    NULL,
[Lng]      FLOAT (53)    NULL

TSQL

use HOM
go
bulk insert HouseInfo
from 'c:\HOM\Houses_of_Mayo_02_02_2015.txt'
with
(
firstrow = 2,
fieldterminator = ',',
FORMATFILE = 'c:\HOM\Format_File.fmt'
)
select *
from HouseInfo

格式文件

10.0
6
1   SQLINT      0   50  ","    2    Id          ""  
2   SQLNCHAR    0   50  ","    3    Name        ""
3   SQLNCHAR    0   100 ","    4    Townland    ""
4   SQLINT      0   50  ","    5    Ref         ""
5   SQLFLT8     0   53  ","    6    Lat         ""
6   SQLFLT8     0   53  "\n"   7    Lng         ""

CSV文件

Id,Name,Townland,Ref,Lat,Lng
A1,Aasleagh Lodge,Srahatloe,1,53.613873,-9.668301
A2,Aasleagh Rectory,Srahatloe,2,53.612478,-9.668033
A3,Abbey Lodge,Carrowkeel (ED Croaghpatrick),3,53.785667,-9.638768
A4,Abbeytown House,Abbeytown,4,54.107701,-9.318194

1 个答案:

答案 0 :(得分:0)

以上代码中我注意到的一些错误。

  1. MouseDownId类型,但此列中csv中的数据为INT
  2. 在您的格式文件中,数据库列的排序从2开始,而不是从1开始。
  3. 您不需要格式文件,您可以直接使用A1,A2...而无需格式文件。

    fieldterminator = ',',rowterminator='\n'