Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Can you edit a field with Identity Specification active?

Posted on 2009-04-01
11
Medium Priority
?
183 Views
Last Modified: 2012-05-06
If you have a field in a table with the identity specification set as "yes"...is there a way to edit that field in the tables?  For example purposes only, MyTable has a field named MyID with identity spec set to yes to start at 20090001 in increments of 1.  So one of the records in this table has a MyID of 20090010, but I want to change this MyID to 20080500.  Is there a way to do this, without writing the script to change the identity spec to no, make the change, and then run the script to set the identify spec back to yes?
0
Comment
Question by:Lee R Liddick Jr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
11 Comments
 

Author Comment

by:Lee R Liddick Jr
ID: 24042218
Increasing the point value because i need to know asap.  Anyone?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24042220
you cannot "update". but you can insert a copy of the record with a new identity value (check out SET IDENTITY_INSERT ON statement), and then delete the old record.
0
 

Author Comment

by:Lee R Liddick Jr
ID: 24042242
Okay that helps, but I've never done that before...can I do a search on the internet on how to prepare that script or do you have any suggestions on how that is written?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 600 total points
ID: 24042420
here we go:
http://msdn.microsoft.com/en-us/library/aa259221(SQL.80).aspx


declare @old_id int
declare @new_id int
 
set @old_id = 20090010 
set @new_id = 20080500
 
set identity_insert MyTable on 
begin transaction
 
insert into MyTable (id, col1, col2 )
 select @new_id, col1, col2
   from Mytable where id = @old_id
 
if @@error = 0
begin
  delete MyTable where id = @old_id
  if @@error = 0
    commit
  else
    rollback
end
else
begin
  rollback
end
 
set identity_insert MyTable off 

Open in new window

0
 
LVL 27

Expert Comment

by:Chris Luttrell
ID: 24042479
The script below shows how to do it.  I created a tmp table, t2, with MyId as an identity like yours and populated it with some records, then used this script to move one of then like you described.  Of course a record with 20080500 can not already exist.
SET IDENTITY_INSERT t2 ON
INSERT INTO t2 (MyId, GroupId, Lvl, Content)
SELECT 20080500, GroupId, Lvl, Content FROM t2 WHERE MyId = 20090010
SET IDENTITY_INSERT t2 OFF
DELETE FROM t2 WHERE MyId = 20090010

Open in new window

0
 

Author Comment

by:Lee R Liddick Jr
ID: 24043068
Excellent example...let me work this out and get back to assign points.
0
 

Author Comment

by:Lee R Liddick Jr
ID: 24043107
Well it worked on my development side because we have admin access to those databases/tables; however, on our production side we do not so it didn't work.  I'm going to have to go through our IT then to fix this.  Unless there is a way to fix this without having admin rights to the database/table.
0
 

Author Comment

by:Lee R Liddick Jr
ID: 24043162
Also, do you have to write this entire script for each record you need to change?  I have 30 that needs changed...can I do that in one script somehow?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24043258
you need the set identit_insert only once fo the entire script.

if you have a table/list of the ID mappings, you can "shortcut" the code, however, as this sounds like one-shot, I would not bother...
0
 
LVL 27

Accepted Solution

by:
Chris Luttrell earned 1000 total points
ID: 24044717
You could make a quick stored procedure and call it the 30 times to make the script cleaner and less chances to mess something up with cut and pasting 30 times.
Again you will have to get someone with proper permissions on production to do it for you since you do not have those permissions.
If you accept this answer, make sure angelIII gets some credit, I used his example since he had transaction coding in it already.
create procedure udfMoveIdentityRecords
 @old_id int,
 @new_id int
as 
 
set identity_insert MyTable on 
begin transaction
 
insert into MyTable (id, col1, col2 )
 select @new_id, col1, col2
   from Mytable where id = @old_id
 
if @@error = 0
begin
  delete MyTable where id = @old_id
  if @@error = 0
    commit
  else
    rollback
end
else
begin
  rollback
end
 
set identity_insert MyTable off 
go
 
-- repeat just this line for each id change 
exec udfMoveIdentityRecords @old_id = 20090010, @new_id = 20080500
 
drop procedure udfMoveIdentityRecords

Open in new window

0
 

Author Closing Comment

by:Lee R Liddick Jr
ID: 31565445
My apologies for not awarding these points sooner...I guess I was all excited everything worked that I forgot.  Thank you both for the assistance!!!
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

715 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