尝试BULK-INSERT时出现一些错误

时间:2015-08-13 10:26:04

标签: sql-server sql-server-2012 bulkinsert bcp

我有一个文本文件,其中包含以下信息:

1,Blabla,Bla,Munich, Entwickler
2,artin,Rosner,Chemnitz,Entwickler
3,Sebastian, Leuoth, Berlin ,Manager
4,bizbiz, bazbaz, Kulalampur, Student

我想使用带有格式文件的BULK插入。我使用bcp对我的格式文件进行了以下格式化,如下所示:

C:\Windows\system32>bcp test.dbo.DimensioList format nul -Uuser1 -Puser1 -n
-f "C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\Listfor
mat.fmt"

如果我运行此代码:

INSERT INTO DimensioList
SELECT * 
FROM
OPENROWSET(BULK 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\List.txt', FORMATFILE='C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\Listformat.fmt') as e

我收到了这些错误:

   Msg 4866, Level 16, State 1, Line 2
    The bulk load failed. The column is too long in the data file for row 1, column 2. Verify that the field terminator and row terminator are specified correctly.  
    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)".

顺便说一句,我的格式文件如下所示:

11.0
5
1       SQLINT              0       4       ""   1     EID                  ""
2       SQLCHAR             2       20      ""   2     Vorname              Latin1_General_CI_AS
3       SQLNCHAR            2       40      ""   3     Nachname             Latin1_General_CI_AS
4       SQLCHAR             2       20      ""   4     Stadt                Latin1_General_CI_AS
5       SQLCHAR             2       20      ""   5     Beruf                Latin1_General_CI_AS

你是什么意思,我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:1)

我更喜欢使用XML格式文件,您可以尝试这样的格式文件: (这适用于您的字段具有固定长度的文件 - 您可以通过终结符轻松地将其用于可变长度...)

<?xml version="1.0"?>
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <RECORD>
    <FIELD ID="1" xsi:type="CharFixed" LENGTH="4"/>
    <FIELD ID="2" xsi:type="CharFixed" LENGTH="20"/>
    <FIELD ID="3" xsi:type="CharFixed" LENGTH="40"/>
    <FIELD ID="4" xsi:type="CharFixed" LENGTH="20"/>
    <FIELD ID="5" xsi:type="CharTerm" TERMINATOR="\r\n"
 </RECORD>
 <ROW>
  <COLUMN SOURCE="1" NAME="EID" xsi:type="SQLSMALLINT"/>
  <COLUMN SOURCE="2" NAME="Vorname" xsi:type="SQLNVARCHAR"/>
  <COLUMN SOURCE="3" NAME="Nachname" xsi:type="SQLNVARCHAR"/>
  <COLUMN SOURCE="4" NAME="Stadt" xsi:type="SQLNVARCHAR"/>
  <COLUMN SOURCE="5" NAME="Beruf" xsi:type="SQLNVARCHAR"/>
 </ROW>
</BCPFORMAT>

将其保存为Listformat.xml,而将其保存为Listformat.fmt