tags:

views:

226

answers:

4
+1  Q: 

Identity insert

Hi,

can anybody please, explain that what does SET IDENTITY INSERT ON AND OFF do.

Thanks, Chris

+3  A: 

SET IDENTITY_INSERT ON

Allows explicit values to be inserted into the identity column of a table.

CMS
Actually it's SET IDENTITY INSERT [tablename] ON/OFF.
Neil Barnwell
+1  A: 

chrisprat, so far you've been registered for a week, and all your activity has been to ask five questions that are easily answered by either Google or MSDN.

What's up?

le dorfier
Chris is a prat, apparently.
ceejayoz
@ceejayoz - I wonder why you felt the need to be rude. It's unecessary and unfunny, where does it get us?
Christopher Edwards
Apparantly stackoverflow is better for chrisprat than google, if it's where he asks the questions first? Others are free not to answer, as they are also free to turn of the TV when nothing is on.
homaxto
+2  A: 

Suppose you have a table with an auto-increment/identity column. You normally cannot specify values for this column when you do inserts since it is automatically populated.

However, if you call SET IDENTITY_INSERT YourTable ON first, you can insert specific values into the identity column.

I use this most often when merging tables manually--I've never used it in a regularly executed block of code.

Also note that you can only specify this on a single table at a time.

Michael Haren
+1  A: 

Michael Haren has given you the answer, but I want to amplify. It is exceedingly dangerous to use this command and you should not use it except in a few very limited cases. When you use it the autonumbering no longer works for anyone, so all code to insert records normally into the database will fail. It therefor should be done in single user mode whenever possible or at the very least during the off hours. It should not be done to regularly insert records but only for things such as merging data from one table to a new structure in a database redesign. If you do not know what you are doing when you use this command you can royally mess up the data integrity of a database.

HLGEM