SQL服务器批量插入空值到时间列(sql server bulk insert nulls into

2019-09-22 14:43发布

我无法从BULK INSERT语句插入空值。

两列空,但编号是身份。

int可空出workd罚款,但time不会。

  • 这里的大部分语句:

     BULK INSERT Circulation FROM '.....file.cs' WITH ( FIRSTROW = 2, MAXERRORS = 0, FIELDTERMINATOR = ',', ROWTERMINATOR = '', KEEPNULLS) 
  • 下面是CSV的摘录:

     ID, IDStopLine, IDException, Hour, PositionHour, Day ,28, 8, 12:20, 52, 0 ,29, 163, , 1, 

意思是说我想插入的两列空值。 其结果是NULL i将int型柱,并且00:00:00时间列

Answer 1:

我不知道为什么时间柱00:00:00插入,但如果你需要NULL,则可以尝试创建触发器并运行FIRE_TRIGGER参数BULK INSERT

CREATE TRIGGER dateNull ON dbo.Circulation
INSTEAD OF INSERT
AS
BEGIN
  INSERT dbo.Circulation(IdStopline, IdException, Hour, PositionHour, Day) 
  SELECT IdStopline, IdException, NULLIF(Hour, '00:00:00.0000000'), PositionHour, Day FROM inserted
END      

BULK INSERT dbo.Circulation 
FROM 'you_file.csv' 
WITH (FIRSTROW = 2, 
      MAXERRORS = 0, FIELDTERMINATOR = ',',
      ROWTERMINATOR = '\n', KEEPNULLS, FIRE_TRIGGERS)


Answer 2:

要插入“NULL”,而不是“col2的默认值”,则需要使用-k开关或KEEPNULL选项,如下面的BCP和BULK INSERT的例子证明。

USE AdventureWorks;
GO
BULK INSERT MyTestDefaultCol2
   FROM 'C:\MyTestEmptyField2-c.Dat'
   WITH (
      DATAFILETYPE = 'char',
      FIELDTERMINATOR = ',',
      KEEPNULLS
   );
GO  

http://msdn.microsoft.com/en-us/library/ms187887.aspx



文章来源: sql server bulk insert nulls into time column