locked
Bulk Insert with Null Value RRS feed

  • Question

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

    The first Row inserted successfully,but the second not.i also checked with CHECK_CONSTRAINTS,but the problem remains

    please help me

    Here is an extract of the csv file:

    First Name, Last Name, Age
    
    Name1,HK,23
    
    Name2,YH, 

    Here the bulk Insert statement:

    Bulk insert dbsct 
    FROM 'C:\Test1\dstd.CSV'
    WITH (
        FIRSTROW = 2,
        FIELDTERMINATOR =',',
        ROWTERMINATOR = '\n'
    KEEPNULLS
        )


    Thursday, December 10, 2015 6:59 PM

Answers

  • Can you upload the file somewhere, so that we can look at the exact bytes? BULK INSERT is a binary tool, and is not the most flexible guy in town. My guess is that there is no terminating CR-LF on the last line.

    Friday, December 11, 2015 12:10 PM

All replies

  • What error message do you get?

    Does the file really have those blank lines?

    How does your table definition look like?

    Thursday, December 10, 2015 10:51 PM
  • (1 row(s) affected).

    no.

    First Name   Varchar(100)

    Last Name   Varchar(100)

    Age   int


    • Edited by Hrishi K Friday, December 11, 2015 9:51 AM
    Friday, December 11, 2015 7:44 AM
  • yes.The file has blank lines.

    Then you should specify the record terminator as \n\n. But if there is no blank line after the last line with data, you will probably lose that record.

    Friday, December 11, 2015 8:51 AM
  • sorry.there is no blank line after the last line

    and also there is no  blank line between the rows

    • Edited by Hrishi K Friday, December 11, 2015 9:53 AM
    Friday, December 11, 2015 9:42 AM
  • I did tests with the data you published and functioned normally.

    Reinforcing the Erland's thesis: How does your table definition look like?

    Friday, December 11, 2015 11:10 AM
  • Table dbsct

    First Name   Varchar(100)

    Last Name   Varchar(100)

    Age   int

    CSV file

    First Name, Last Name, Age Name1,HK,23 Name2,YH,

    Bulk insert dbsct 
    FROM 'C:\Test1\dstd.CSV'
    WITH (
        FIRSTROW = 2,
        FIELDTERMINATOR =',',
        ROWTERMINATOR = '\n'
    KEEPNULLS
        )

    The last row not added


    • Edited by Hrishi K Friday, December 11, 2015 11:34 AM
    Friday, December 11, 2015 11:30 AM
  • Make sure to keep an space in the NULL field when it is the last column(Age column in the second row).


    Friday, December 11, 2015 11:41 AM
  • now its working.

    But the csv file is already generated.i can't modify it.

    there is any other option

    Thanks for your valuable reply.


    • Edited by Hrishi K Friday, December 11, 2015 11:52 AM
    Friday, December 11, 2015 11:52 AM
  • Can you upload the file somewhere, so that we can look at the exact bytes? BULK INSERT is a binary tool, and is not the most flexible guy in town. My guess is that there is no terminating CR-LF on the last line.

    Friday, December 11, 2015 12:10 PM
  • Use Notepad++ to replace the expression ",\r\n" by ", \r\n".

    Ignore quotation marks in expression.

    Friday, December 11, 2015 2:34 PM