Keep getting import error “String data, right truncation” using BCP

£可爱£侵袭症+ 提交于 2021-01-29 16:47:07

问题


I've hit a problem. I'm trying to use BCP to import about 700,000 records into a working table.

This is the SQL I use to build my working table:

If Exists (Select * From sys.tables Where [name] = 'InactiveIDs')
    Begin 
        Drop Table [dbo].[InactiveIDs]
    End

Create Table [dbo].[InactiveIDs] (
    ContactId UniqueIdentifier Not Null,
    ID Varchar(50),
    EmailAddress VarChar(255) Not Null
)

Create Index IX_ContactIdEmailAddress
    On [dbo].[InactiveIDs] (ContactId, EmailAddress)

Here is the format file file:

13.0
3
1       SQLUNIQUEID         1       36      ","        1     ContactId                    ""
2       SQLCHAR             2       50      ","        2     ID                           SQL_Latin1_General_CP1_CI_AS
3       SQLCHAR             2       255     "\r\n"     3     EmailAddress                 SQL_Latin1_General_CP1_CI_AS

Here is some sample data:

CBD60121-C5E1-E511-B2B4-005056820129,199e6799e3c64b06a87e86a5047e5f41,someone@verizon.com
3D22A4C2-507B-E411-99C7-005056820126,76410ce5beab4a7da943b95b3de3b0c1,someone@gmail.com
AE5B9335-B126-E611-ABF2-005056820020,e7d181abdf154f79b5dcaa4d64fec7f7,someone@yahoo.com
93F94F65-FA2A-E311-87A7-005056B5025F,3e2fae28cace4f068fa670879d7807e3,someone@juno.com
0A41305C-C087-E411-A37D-00505682001E,55b0162742b04a369c1c57d8d917d45c,someoneelse@yahoo.com

Here's my command line:

bcp.exe dbo.InactiveIDs in InactiveIDs.csv -f InactiveIDs.bcp.fmt -T -S SqlServerName -d DatabaseName

Here is the output:

Starting copy...
SQLState = 22001, NativeError = 0
Error = [Microsoft][ODBC Driver 13 for SQL Server]String data, right truncation
SQLState = 22001, NativeError = 0
Error = [Microsoft][ODBC Driver 13 for SQL Server]String data, right truncation
SQLState = 22001, NativeError = 0
Error = [Microsoft][ODBC Driver 13 for SQL Server]String data, right truncation
SQLState = 22001, NativeError = 0
Error = [Microsoft][ODBC Driver 13 for SQL Server]String data, right truncation
SQLState = 22001, NativeError = 0
Error = [Microsoft][ODBC Driver 13 for SQL Server]String data, right truncation
SQLState = 22001, NativeError = 0
Error = [Microsoft][ODBC Driver 13 for SQL Server]String data, right truncation
SQLState = 22001, NativeError = 0
Error = [Microsoft][ODBC Driver 13 for SQL Server]String data, right truncation
SQLState = 22001, NativeError = 0
Error = [Microsoft][ODBC Driver 13 for SQL Server]String data, right truncation
SQLState = 22001, NativeError = 0
Error = [Microsoft][ODBC Driver 13 for SQL Server]String data, right truncation
SQLState = 22001, NativeError = 0
Error = [Microsoft][ODBC Driver 13 for SQL Server]String data, right truncation

BCP copy in failed

回答1:


The official Microsoft doc explains what a Non-XML Format File consist of. I see 2 possible problems with yours, both with the first field:
1. Column 4 (Host file data length) should be 37, if you open the page that explains each data type and you go to uniqueidentifier, it says 37 - now I copy the values from your sample data till the first comma to Notepad and they are 37 characters long.
2. Maybe the type SQLUNIQUEID is also wrong and should be SQLCHAR. The doc says for columns 2 & 3 that these should be the same as the ones used to do the extract. I don't know if the extract was done with native data type but it seems to me that the first field is also text. So if point 1. modification still fails I would try SQLCHAR with a Prefix length of zero for all fields.




回答2:


Something is most definitely getting truncated. Never seen this error be wrong. Only thing is data can be getting truncated for a few different reasons.

  1. Your column sizes seem adequate. However, that is just with the sample data you've shared. If this is a very large file, it is possible that some other values are longer than your sample? Does the BCP complete any batches? (do yousee "1000 records written..." over and over?)
  2. There may be a problem with delimiters. If the delimiters you specify (column or line delimiters) do not match what is actually in the file... then SQL Server/BCP will keep ingesting data until it finds the delimiter you tell it to look for. You seem to be using commas and your sample data has them...but the line terminator? Are you sure it's CRLF? Sometimes I see files with just LF, or even some other odd, not visible control character.
  3. No matter what you find with 1 and 2 in mind above, you should include an error file using the -e option. This is a very handy output that will give you line numbers and the data from any lines offending. Only returns the top 10 error'd rows.

My guess is that you have some larger values than what you see in your sample. You can also try pasting yoru sample into a new file...just those sample rows and try loading that. If that works, then you ahve some data further down in your file that your architecture is not anticipating. Likely is that or you have an odd line terminator. Do you have a good text file viewer? One that shows control characters that are normally hidden in the basic "notepad" in Windows?



来源:https://stackoverflow.com/questions/53178009/keep-getting-import-error-string-data-right-truncation-using-bcp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!