sql server bulk insert nulls into time column

蓝咒 提交于 2020-01-03 09:23:42

问题


I'm having trouble inserting null value from a bulk insert statement.

Two columns are nullable and the Id is identity.

The int nullable workd out fine, but the time doesn't.

  • Here the bulk statement:

    BULK INSERT Circulation 
    FROM '.....file.cs' 
    WITH ( FIRSTROW = 2, MAXERRORS = 0, FIELDTERMINATOR = ',', 
         ROWTERMINATOR = '', KEEPNULLS)
    
  • Here is an extract of the csv:

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

Meaning that I'm trying to insert nulls in those both columns. The result is ithe int column with NULL and the time column with 00:00:00


回答1:


I don't know why time column insert with 00:00:00, but if you need NULL then it is possible to try create trigger and run BULK INSERT with FIRE_TRIGGER argument

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)



回答2:


To insert "NULL" instead of "Default value of Col2", you need to use the -k switch or KEEPNULL option, as demonstrated in the following bcp and BULK INSERT examples.

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



来源:https://stackoverflow.com/questions/12494391/sql-server-bulk-insert-nulls-into-time-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!