批量加载转换错误(截断)......遗漏了一些明显的东西

时间:2017-09-06 14:02:30

标签: sql sql-server bulkinsert

我有一个像这样定义的表:

CREATE TABLE [ThirdPartyData].[AdsTxtFile](
    [DomainHostName] [varchar](200) NOT NULL,
    [Expires] [datetime] NOT NULL,
    [Advertiser] [varchar](200) NOT NULL,
    [AdvertiserId] [varchar](50) NOT NULL,
    [AdvertiserType] [varchar](20) NOT NULL,
    [TAGId] [varchar](100) NULL,
    CONSTRAINT [PK_AdsTxtFile] PRIMARY KEY CLUSTERED 
        (
        [DomainHostName] ASC,
        [Advertiser] ASC,
        [AdvertiserId] ASC
        )
)

我正在尝试将文件批量插入此表。该文件的前几行是:

roadandtrack.com(TAB)9/13/2017 12:00:00 AM(TAB)google.com(TAB)pub-1301581561755234(TAB)DIRECT(TAB)f08c47fec0942fa0
roadandtrack.com(TAB)9/13/2017 12:00:00 AM(TAB)indexexchange.com(TAB)184046(TAB)DIRECT(TAB)
roadandtrack.com(TAB)9/13/2017 12:00:00 AM(TAB)indexexchange.com(TAB)183430(TAB)DIRECT(TAB)
roadandtrack.com(TAB)9/13/2017 12:00:00 AM(TAB)indexexchange.com(TAB)184124(TAB)DIRECT(TAB)
roadandtrack.com(TAB)9/13/2017 12:00:00 AM(TAB)indexexchange.com(TAB)182270(TAB)DIRECT(TAB)
roadandtrack.com(TAB)9/13/2017 12:00:00 AM(TAB)appnexus.com(TAB)7287(TAB)DIRECT(TAB)
roadandtrack.com(TAB)9/13/2017 12:00:00 AM(TAB)amazon-adsystem.com(TAB)3049(TAB)DIRECT(TAB)
roadandtrack.com(TAB)9/13/2017 12:00:00 AM(TAB)Kargo.com(TAB)114(TAB)DIRECT(TAB)
roadandtrack.com(TAB)9/13/2017 12:00:00 AM(TAB)Kargo.com(TAB)115(TAB)DIRECT(TAB)
roadandtrack.com(TAB)9/13/2017 12:00:00 AM(TAB)openx.com(TAB)537114802(TAB)DIRECT(TAB)a698e2ec38604c6
roadandtrack.com(TAB)9/13/2017 12:00:00 AM(TAB)rubiconproject.com(TAB)7791(TAB)DIRECT(TAB)0bfd66d529a55807
roadandtrack.com(TAB)9/13/2017 12:00:00 AM(TAB)rubiconproject.com(TAB)15896(TAB)DIRECT(TAB)0bfd66d529a55807
roadandtrack.com(TAB)9/13/2017 12:00:00 AM(TAB)rubiconproject.com(TAB)11636(TAB)DIRECT(TAB)0bfd66d529a55807
roadandtrack.com(TAB)9/13/2017 12:00:00 AM(TAB)teads.tv(TAB)10237(TAB)DIRECT(TAB)15a9c44f6d26cbe1

我已将文件中的标签字符替换为(TAB),以便在此帖子中显示,但它们是文件本身中的实际标签字符。文件中的每一行都以换行符结束。

我在SQL Server中运行以下命令:

BULK INSERT [ThirdPartyData].[AdsTxtFile] FROM 'MyFileLocation.txt'

我得到如下例外:

System.Data.SqlClient.SqlException (0x80131904): Bulk load data conversion error (truncation) for row 1, column 6 (TAGId).
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()

...现在我正在摸不着头脑,因为我的理解是标签和换行符是批量插入命令的默认终止符,第一行TAGId的“f08c47fec0942fa0”肯定没有超过100个字符,所以我不知道问题是什么。谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:0)

好吧,我读过的文档建议默认行终止是换行符或换行符(\ n)......但似乎并非如此。一旦我将行结束更改为回车换行(\ r \ n),它就能正常工作。