Disable SQL Server Column Identity

Asked

Viewed 23,959 times

6

How do I disable the property identity of a column in the SQL Server?

I’ve tried to:

SET IDENTITY_INSERT ON/OFF

But it didn’t work. I don’t know if it’s only for insertion, but I need to do an update.

  • To update you do not need to disable Identity. as you are trying to do your update?

  • Thus: UPDATE product SET id_product = 500 WHERE id_product = 1; I get the following error: Message 8102, Level 16, Status 1, Line 1 Cannot update identity column 'id_product'.

  • you rode the command SET IDENTITY_INSERT ON product

  • To disable, I believe it is SET IDENTITY_INSERT ON, correct?

  • Yes, I had edited.

3 answers

8

Ex.1:

SET IDENTITY_INSERT masterTbl ON  --Desabilita o IDENTITY

INSERT INTO masterTbl (id, name) VALUES (1, 'MNO') --Consegue inserir

SET IDENTITY_INSERT masterTbl OFF  --Habilita o IDENTITY

Ex.2:

SET IDENTITY_INSERT produto ON --Desabilita o IDENTITY

insert into produto (Codigo, Nome) --Consegue inserir
SELECT Codigo, Nome
from produto2
where codigo in (46296,46298,46301)

SET IDENTITY_INSERT produto OFF --Habilita o IDENTITY

OBS: INSERT COLUMNS NEED TO BE SPECIFIED IF IT IS AN INSERT INTO SELECT, BOTH INSERT AND SELECT COLUMNS NEED TO BE SPECIFIED.

3

First of all, you don’t want disable Identity Insert, you want enable him.

There’s no way to do this without "workarounds" (That’s right, you’ll have to do a gambiarra).

You can use a Insert Into Select (i don’t know what it’s called true) to create an identical record with the new id, and then delete the old one.

Take my example, I change the value of Id of 1 for 500, is the best way I can think now.

Note that this won’t work if there are FK’s with reference to this id being exchanged.

Set Identity_insert Tabela On
Go

Insert Into Tabela (Id, Nome, Etc, Etc2) 
     Select 500, (Nome, Etc, Etc2) -- 500 é o novo Id
     From Tabela Where Id = 1 -- 1 é o Id antigo

Delete Tabela Where Countryid = 1 

Set Identity_insert Geocountry Off
Go
  • 'Cause I don’t need to insert, I need to edit, at first.

  • It’s impossible, I told you. You’ll need to enter an identical record with another id (as I do in my reply).

  • Yeah, I’m gonna have to find another way to solve this problem of mine.

  • What’s wrong with my solution?

  • I have to take the value of another column and it can be equal to an ID that already exists. But actually, it’s not even in your solution itself. I’m gonna have to rethink a way.

  • I think the solution Good too...

Show 1 more comment

0

You need to temporarily authorize the insertion of identity values, to do so in SQL Server Management Studio right-click on the table and click Design as shown below.

inserir a descrição da imagem aqui

After that click on the PK of the table.

inserir a descrição da imagem aqui

then change the field (Is indentity) to No.

inserir a descrição da imagem aqui

Remove your primary key.

inserir a descrição da imagem aqui

Save the change and then update.

  • Are you sure it is possible to upgrade Identity columns?

  • It doesn’t really work. Maybe it’s not possible, as @jbueno said...

  • That’s what’s in the prints I’ve done too

  • @elvex , If the last change doesn’t resolve you’re probably doing something very wrong

Browser other questions tagged

You are not signed in. Login or sign up in order to post.