2015-08-13 78 views
0

我有一個文本文件,它包含以下相關信息:一些錯誤,而試圖BULK-INSERT

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

我想用BULK INSERT,具有格式文件。我已經generetade我的格式文件,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 

你是什麼意思,我該怎麼做才能解決這個問題?

+0

我認爲這是因爲列Vorname在DimensioList表的格式。 「Blabla」似乎對這個專欄來說太大了。 – Polux2

回答

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