question

keentolearn avatar image
keentolearn asked

Bulk inserting a number set to a table with an identity column

Hi there, I want to add/create 300 rows in a able with an idendity column. I want to give specify the values for the identity column, e.g. I want to insert 300 rows with identity number 55 to 355

I can do this,

SET IDENTITY_INSERT [MYTABLE] ON INSERT INTO MYTABLE (id, creator) values 55, 'Autoprocess' SET IDENTITY_INSERT [MYTABLE] OFF

however, I would need to do this 300 times. Is there a way to insert all 55 to 355 in one query?

Many thanks.

insertbulk-insertidentity
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Piotr Rodak avatar image
Piotr Rodak answered

You can set IDENTITY_INSERT once, add all your rows and then switch the IDENTITY_INSERT off.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.