5

我有以下代码可以批量插入到 sql server 中(取自此处底部的示例):

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 文件末尾有一个额外的行,因此它试图在到达文件末尾时插入一个空白行(并且所有其他行都为它们设置了标识。 )

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

4

1 回答 1

3

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

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
于 2013-07-26T13:10:29.203 回答