SQL won't insert null values with BULK INSERT

后端 未结 2 1471
难免孤独
难免孤独 2020-12-19 02:01

I have a CSV file and each line looks similar to this:

EASTTEXAS,NULL,BELLVILLE AREA,NULL,BELLVILLE AREA,RGP,NULL,NULL,0,NULL,NULL,NULL,1,1,PM,PM Settings,NU         


        
2条回答
  •  误落风尘
    2020-12-19 02:45

    According to:

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

    null values can be inserted by having an empty field within your file.

    Example file was:

    1,,DataField3
    2,,DataField3
    

    Example method of importing file keeping nulls is:

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

    Granted, this means you'll have to change your "NULL"s to "", and any empty strings you did want as empty string would be interpreted as nulls, but it might be enough to get you started? I would imagine to keep your empty string columns they would need to be changed from

    field1,,field2
    

    to

    field1,"",field2
    

    as example

提交回复
热议问题