SQL Server:BULK INSERT FAILED IID_IColumnsInfo

时间:2017-10-09 09:59:53

标签: sql-server csv bulk

我在procStock中使用BULK插入,并收到此错误消息:

  

Msg 4866,Level 16,State 8,Line 2
  批量加载失败。第1行第1列的数据文件中的列太长。验证是否正确指定了字段终止符和行终止符。

     

Msg 7301,Level 16,State 2,Line 2
  无法从OLE DB提供程序“BULK”获取链接服务器“(null)”所需的接口(“IID_IColumnsInfo”)。

我在Stack中查找帖子并找到一些使用ROWTERMINATOR ='0x0a'的解决方案建议,但我仍然遇到同样的错误。

    SET @sir_de_executat = '
    BULK INSERT #test
    FROM ''d:\scripts\test.csv''
    WITH (
        FIRSTROW=2
        , FORMATFILE=''d:\scripts\aaa2.fmt''
        , FIELDTERMINATOR = '';''
        , ROWTERMINATOR = ''0x0a''
    )'

感谢您的帮助。

这是一个csv示例

BPARTNER;ME_TITLE_0208;BP_LSTNAME_0209;BP_FSTNAME_0210;STR_SUPPL1_0211;STR_SUPPL2_0212;HOUSE_NUM1_0213;HOUSE_NUM2_0214;STREET60_0215;STR_SUPPL4_0216;POSTALCODE_0217;CITY_1_0218;TEL_NUMBER_0219;FAX_NUMBER_0220;ZCHASSNR;ZFAHRZEUG__ZBRAND;ZFAHRZEUG__ZMODELKEY;ZFAHRZEUG__ZFISCHP;ZFAHRZEUG__ZMOTRVAR;ZFAHRZEUG__ZZULASSG;ZCREGDAT;/BIC/ZDDEALER;/BIC/Z_REGRNO_0124;ZFAHRZEUG__ZMOTR_KB4;ZFAHRZEUG__ZMODJAHR
6016562635;0002;FOO;BAR;;;823;;RUE DE LA SOUTE;;60400;CUTS;;;AZEZZZ8U0HR067422;AU;8UGBGY;0008;D;20170803;20170803;FRAA01612;EP-803-RP;DFTA;2017

这是fmt格式文件

10.0
25
1       SQLCHAR             0       20      "\t"      1     bpartner                                               SQL_Latin1_General_CP1_CI_AS
2       SQLCHAR             0       30      "\t"      2     title                                                  SQL_Latin1_General_CP1_CI_AS
3       SQLCHAR             0       256     "\t"      3     last_name                                              SQL_Latin1_General_CP1_CI_AS
4       SQLCHAR             0       256     "\t"      4     first_name                                             SQL_Latin1_General_CP1_CI_AS
5       SQLCHAR             0       256     "\t"      5     street2                                                SQL_Latin1_General_CP1_CI_AS
6       SQLCHAR             0       256     "\t"      6     street3                                                SQL_Latin1_General_CP1_CI_AS
7       SQLCHAR             0       60      "\t"      7     house_number                                           SQL_Latin1_General_CP1_CI_AS
8       SQLCHAR             0       60      "\t"      8     house_number_completion                                SQL_Latin1_General_CP1_CI_AS
9       SQLCHAR             0       256     "\t"      9     street                                                 SQL_Latin1_General_CP1_CI_AS
10      SQLCHAR             0       256     "\t"      10    street5                                                SQL_Latin1_General_CP1_CI_AS
11      SQLCHAR             0       30      "\t"      11    postal_code                                            SQL_Latin1_General_CP1_CI_AS
12      SQLCHAR             0       256     "\t"      12    city                                                   SQL_Latin1_General_CP1_CI_AS
13      SQLCHAR             0       128     "\t"      13    tel_number                                             SQL_Latin1_General_CP1_CI_AS
14      SQLCHAR             0       128     "\t"      14    fax_number                                             SQL_Latin1_General_CP1_CI_AS
15      SQLCHAR             0       50      "\t"      15    chassis_number                                         SQL_Latin1_General_CP1_CI_AS
16      SQLCHAR             0       10      "\t"      16    brand                                                  SQL_Latin1_General_CP1_CI_AS
17      SQLCHAR             0       15      "\t"      17    model_key                                              SQL_Latin1_General_CP1_CI_AS
18      SQLCHAR             0       15      "\t"      18    fiscal_horse_power                                     SQL_Latin1_General_CP1_CI_AS
19      SQLCHAR             0       60      "\t"      19    engine_option                                          SQL_Latin1_General_CP1_CI_AS
20      SQLCHAR             0       24      "\t"      20    first_registration_date                                ""
21      SQLCHAR             0       24      "\t"      21    current_registration_date                              ""
22      SQLCHAR             0       100     "\t"      22    sales_dealer                                           SQL_Latin1_General_CP1_CI_AS
23      SQLCHAR             0       100     "\t"      23    licence_plate                                          SQL_Latin1_General_CP1_CI_AS
24      SQLCHAR             0       60      "\t"      24    engine_code                                            SQL_Latin1_General_CP1_CI_AS
25      SQLCHAR             0       12      "\r\n"    25    year                                                   ""

1 个答案:

答案 0 :(得分:0)

在创建fmt文件时,必须将Field Terminator指定为与CSV文件中使用的相同。 为bcp command提供了文档,您必须指定-t选项

bcp AdventureWorks..myDepartment in C:\myDepartment-c-t.txt -c -t ; -r \n -T

this documentation中所述,默认字段终止符是\t,这似乎是问题所在。