Insert and set the Identity value using SET IDENTITY_INSERT


InterServer Web Hosting and VPS
Sony Playstation PS4 Pro Custom Build NVMe SSD Drive - Enclosure case and Crucial NVMe SSD
PS4 Pro Custom External SSD NVMe Drive

How I built a PS4 Pro Custom External SSD NVMe Drive...

NUC8i7BEH with memory
Intel NUC8I7BEH Review and Custom Build

This is my review of the Intel NUC8i7BEH and a summary of my build...

Card image cap
Asus M5A88-M EVO Review

This is my review of the Asus M5A88-M EVO motherboard...

Corsair Vengeance Blue 8GB (2x4GB) DDR3 PC3-12800C9 1600MHz Dual Channel Kit
Corsair Vengeance Blue Review

This is my review of the Corsair Vengeance Blue 8GB (2x4GB) DDR3 PC3-12800C9 1600MHz Dual Channel Kit memory...

Robotic Arm with USB PC Interface Review
Robotic Arm with USB PC Interface Review

This is my review of the Robotic Arm with USB PC Interface...

Insert and set the Identity value using SET IDENTITY_INSERT

Here we look at how to insert records and set the ID value, on a table which creates ID values automatically.

We need to use the SET IDENTITY_INSERT command, setting it to ON, make the insert, then switch it to OFF.

Here is an example where we are inserting a record with a specific value, we are actually selecting a record from the same table and then inserting a copy of the values as a new record, but with a new ID of -1:


SET IDENTITY_INSERT [Sections] ON

insert into [dbo].[Sections] (id,
[SectionTitle]
,[FileName]
,[TimeStamp])
SELECT -1
,[SectionTitle]
,[FileName]
,[TimeStamp]
FROM [dbo].[Sections]
where id = 12

SET IDENTITY_INSERT [Sections] OFF


Sponsored Links
SolarSystem - AngularJS Demo
SolarSystem - Angular JS Demo

Check out my first Angular JS demo hosted in Azure, it helped win my first Angular contract. Put the sound on.