我有以下代码来批量插入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文件的末尾有一个额外的行,所以当它到达文件的末尾时它试图插入一个空行(并且所有其他行都有一个标识集对他们来说。)
有没有办法让它正常工作,因为我有多个大型dbs,我试图使用它?
答案 0 :(得分:2)
事实证明我需要在查询中添加正确的参数。最后,这是对我有用的查询:
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