1. 程式人生 > >SQL Server: Text was truncated or one or more characters had no match in the target code page error

SQL Server: Text was truncated or one or more characters had no match in the target code page error

匯入CSV到SQL Server真是一件美好的事情,這是轉自一個外國人部落格裡的部分解決方案。但我想吐槽的遠不止這個。

1. 如果你的csv比較乾淨,也就是指一項資料中沒有逗號“,”的話,通過下面的方法改改欄位長度就好了(微軟你們真懶啊,遍歷一遍自動設個長度不行啊)

When trying to import a CSV file into a database using the “Import Data” SSIS option in SQL Server Management Studio I kept getting this error:

“Text was truncated or one or more characters had no match in the target code page.”

This was driving me nuts – such a simple thing to do and the tool failed each time. Turns out that, for whatever reason, and despite the data import wizard realising the target table has enough room in the columns, that you have to click on the Advanced tab and say what the size of the destination table columns are going to be (this is before you have selected the destination of course).

sqlserver-dataimport-settings

Once you’ve done this the import should now work perfectly.

You would have thought by now that Microsoft would have made this a bit more intelligent by now…

2. 如果不幸的,你的資料比較複雜,比如某一行資料是這樣的

     北京大學, 2012, 40000, "北京,海淀區"

    以我目前的測試情況是,最後的"北京,海淀區"會被切成兩個資料,即 ("北京) 和 (海淀區"),引號MS你都不處理讓人哭啊。

    折中方案,轉換成excel或者其他格式再進行匯入,這樣sql server就會把資料項區分正確,運氣好的話就走通了。

3. 如果還很不幸,和我一樣,導成excel之後還是這個問題,還是報告資料超過欄位長度,那麼,我目前還沒解決~我嘗試過把對應欄位改成text但依然報錯,試試再曲線救國轉成別的可能不會報錯的格式再匯入吧。