Solved

SET IDENTITY_INSERT ON won't let me UPDATE an Identity column

Posted on 2004-08-17
5
1,445 Views
Last Modified: 2012-08-14
hi,

i'm hoping to update an Identity column by turning off identity briefly, updating my data, then turning identity back on.
i know i can insert when i turn IDENTITY_INSERT ON, but can't UPDATE - is there an equivalent available in SQL Server 2000 T-SQL that will let me UPDATE, without having to go turn identity off for the column, UPDATE, then turn it back on?

no crazy rush on this, hence the no crazy point allocation...and i suspect the answer is 'can't be done', but would love to hear otherwise!

thanks,
0
Comment
Question by:gdoherty
  • 2
5 Comments
 
LVL 10

Expert Comment

by:AaronAbend
ID: 11819526
It cannot be done. It is in the manual for Sybase, and that has the same architecture as MS SQL Server. It might be in MS's doc as well, somewhere.
0
 
LVL 10

Assisted Solution

by:AaronAbend
AaronAbend earned 70 total points
ID: 11819596
Naturally you could rebuild the table

create table newtab ... (same structure without identity specified)
insert newtab select * from oldtab
exec sp_rename originaltabname, savetab
exec sp_rename newtab, originaltabname

(Note that some operations that Enterprise Manager "lets" you do will do a table rebuild in exactly this way. Since update is not an EM function, that does not apply specifically here, but removing identity from a column would be. Click the button that shows the SQL before you execute it in EM and it will show you the rebuild).

0
 
LVL 3

Accepted Solution

by:
alexpreston earned 55 total points
ID: 11820588
You could turn identity insert on, re-insert the existing row with a new id, and then delete the old one.
e.g. for new id of 10, old id 3, something like...
INSERT INTO [table]
(SELECT 10, field2, field3 FROM [table] WHERE field1 = 3)
0
 

Author Comment

by:gdoherty
ID: 12110929
thanks guys,

seems to be a hole in ms sql server...would be handy, but your answers have shown me that can't be done.

many thanks/sorry for the delay.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
export sql results to csv 6 34
SQL Help 27 41
SQL Error - Query 6 24
VB.NET 2008 - SQL Timeout 9 23
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question