【问题标题】:Bulk insert failing: Explicit value must be specified批量插入失败:必须指定显式值
【发布时间】:2013-07-26 09:36:52
【问题描述】:

我有以下代码可以批量插入到 sql server (taken from the example at the bottom of here):

SET IDENTITY_INSERT [dbo].Reporting_DriverScoreCard ON

EXECUTE XP_CMDSHELL 'BCP AT100Reporting.dbo.Reported_Driver_ScoreCard out D:\temp\Reported_Driver_ScoreCard.txt -T -n'
BULK INSERT [dbo].Reporting_DriverScoreCard 
FROM 'D:\temp\Reported_Driver_ScoreCard.txt'
WITH 
(
    DATAFILETYPE  = 'native',
    ERRORFILE = 'D:\temp\error.txt',
    MAXERRORS = 10000 
);

SET IDENTITY_INSERT [dbo].Reporting_DriverScoreCard OFF

但是,当我运行这个命令时,它会失败并给我这个错误消息:

Explicit value must be specified for identity column in table 'Reporting_DriverScoreCard' either when IDENTITY_INSERT is set to ON or when a replication user is inserting into a NOT FOR REPLICATION identity column.

我认为这是因为正在创建的 txt 文件末尾有一个额外的行,因此它试图在到达文件末尾时插入一个空白行(并且所有其他行都有一个标识集给他们。)

有什么方法可以让它正常工作,因为我有多个大型数据库,我正在尝试使用它?

【问题讨论】:

  • 你确认那里有 多行吗?该文件是否像消息告诉您的那样提供身份值? BULK INSERT 在几秒钟内执行的速度有多快?它是立即失败还是最终失败?
  • 它最后失败了,我已将以下值添加到语句 -e 到 BCP 和 KEEPIDENTITY 到批量插入但我无法验证文件,因为它太大了打开
  • 使用十六进制编辑器或仅将文件的第一个和最后一个 meg 复制到新位置。或者使用视图或查询输出更少的数据。

标签: sql-server bulkinsert bcp


【解决方案1】:

事实证明,我需要将正确的参数添加到查询中。最后,这是对我有用的查询:

EXECUTE XP_CMDSHELL 'BCP AT100Reporting.dbo.Reported_Driver_ScoreCard out D:\temp\Reported_Driver_ScoreCard.dat -T -E -n -k'

SET IDENTITY_INSERT [dbo].Reporting_DriverScoreCard ON

BULK INSERT [dbo].Reporting_DriverScoreCard 
FROM 'D:\temp\Reported_Driver_ScoreCard.dat'
WITH 
(
    KEEPIDENTITY,
    BATCHSIZE = 5000,
    DATAFILETYPE  = 'native',
    ERRORFILE = 'D:\temp\error.txt',
    MAXERRORS = 10000,
    KEEPNULLS
);

SET IDENTITY_INSERT [dbo].Reporting_DriverScoreCard OFF

【讨论】:

    猜你喜欢
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    • 2016-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    相关资源
    最近更新 更多