BULK INSERT with identity (auto-increment) column

前端 未结 9 1292
情话喂你
情话喂你 2020-11-29 02:57

I am trying to add bulk data in database from CSV file.

Employee table has a column ID (PK) auto-incremented.

CREATE TABLE [dbo].[Employ         


        
相关标签:
9条回答
  • 2020-11-29 03:56

    Add an id column to the csv file and leave it blank:

    id,Name,Address
    ,name1,addr test 1
    ,name2,addr test 2
    

    Remove KEEPIDENTITY keyword from query:

    BULK INSERT Employee  FROM 'path\tempFile.csv ' 
    WITH (FIRSTROW = 2,FIELDTERMINATOR = ',' , ROWTERMINATOR = '\n');
    

    The id identity field will be auto-incremented.

    If you assign values to the id field in the csv, they'll be ignored unless you use the KEEPIDENTITY keyword, then they'll be used instead of auto-increment.

    0 讨论(0)
  • 2020-11-29 04:02

    Don't BULK INSERT into your real tables directly.

    I would always

    1. insert into a staging table dbo.Employee_Staging (without the IDENTITY column) from the CSV file
    2. possibly edit / clean up / manipulate your imported data
    3. and then copy the data across to the real table with a T-SQL statement like:

      INSERT INTO dbo.Employee(Name, Address) 
         SELECT Name, Address
         FROM dbo.Employee_Staging
      
    0 讨论(0)
  • 2020-11-29 04:02

    Another option, if you're using temporary tables instead of staging tables, could be to create the temporary table as your import expects, then add the identity column after the import.

    So your sql does something like this:

    1. If temp table exists, drop
    2. Create temp table
    3. Bulk Import to temp table
    4. Alter temp table add identity
    5. < whatever you want to do with the data >
    6. Drop temp table

    Still not very clean, but it's another option... might have to get locks to be safe, too.

    0 讨论(0)
提交回复
热议问题