Do a bulk instert on a table with an auto increment id field

Christopher Jack 1,616 Reputation points
2021-01-25T11:17:44.917+00:00

Hi,

I have a table called Palletinfo .

I am trying to do a bulk insert on the table using a CSV and the following code

BULK INSERT Palletinfo
FROM 'C:\test\Palletlinfo.csv'
WITH (FIRSTROW = 2,
    FIELDTERMINATOR = ',',
    ROWTERMINATOR='\n'
 );	

How do I get it to skip the first ID field which is the auto increment one and insert the info into the second and third column on the database?

At the moment it is trying to upload the info into the first column.

Thanks

Developer technologies | Transact-SQL
SQL Server | Other
0 comments No comments
{count} votes

3 additional answers

Sort by: Most helpful
  1. Yitzhak Khabinsky 26,586 Reputation points
    2021-01-25T13:13:38.76+00:00

    The easiest way is to create a view (let's say vw_Palletinfo) without the

    ID field which is the auto increment

    After that just modify your statement as follows:

    BULK INSERT vw_Palletinfo
     FROM 'C:\test\Palletlinfo.csv'
     WITH (FIRSTROW = 2,
         FIELDTERMINATOR = ',',
         ROWTERMINATOR='\n'
      );  
    
    1 person found this answer helpful.
    0 comments No comments

  2. Tom Phillips 17,771 Reputation points
    2021-01-25T14:10:11.8+00:00

    The best way to use BCP/Bulk Insert is to insert into a temp or "stage" table, then merge the data from the stage table into the target table. So the stage table does not have an identity field.

    1 person found this answer helpful.
    0 comments No comments

  3. Christopher Jack 1,616 Reputation points
    2021-01-25T11:24:01.86+00:00

    Found the answer, you have to have the id as a blank column in the CSV file.

    Is there any way to it in the code rather than on the csv?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.