BULK INSERT and primary key

  • I want to BULK INSERT a .csv file to a table and have the table autogenerate a PK for each row. Is there a way to do this directly? Or would I have to use a staging table?

  • by "auto-generate primary key", i assume you're referring to an identity column. if so, we typically add the identity column as the last column of the table, then create a bcp fmt file and remove the identify column from the fmt file. using that fmt file, you'll be able to bcp in the data to the table and the identity column will be populated.

  • Hi,

    As long as the column on the table has the identity property turned on, this will autogenerate a value for each row imported.

    Thanks,

    Phillip Cox

    MCITP - DBAdmin

  • Hi,

    As long as the column on the table has the identity property turned on, this will autogenerate a value for each row imported.

    You still need the format file, so that bcp does not try to import into the identity column.

    Edit: resolved a bad closing quote tag.

  • Thanks all! I'll get started with a bcp.fmt

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply