Solved

Is there a way to manually change/update  an ID number in SQL Server Management Studio

Posted on 2010-11-18
8
260 Views
Last Modified: 2012-05-10
The ID number are auto-incremental and some rows were deleted so there are numbers in between where there are a couple of instances were I would like to change one or two of those ID numbers for continuity but is grayed out. Is there a way to do this with the GUI interface?

For example I have rows

1
4
5
6
7

I would like to change the 4 to a 2 and the 5 to a 3.
ID is the primary key and is set to "identity".

Thank you in advance.
0
Comment
Question by:colonelblue
[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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 19

Accepted Solution

by:
Bhavesh Shah earned 375 total points
ID: 34166554
Hi,

You can do that by

DBCC CHECKIDENT('YourTableName', RESEED, 1)
0
 
LVL 28

Assisted Solution

by:sammySeltzer
sammySeltzer earned 125 total points
ID: 34166558
use identity reseed

DBCC CHECKIDENT (yourtable, reseed, startnumber)


0
 
LVL 19

Assisted Solution

by:Bhavesh Shah
Bhavesh Shah earned 375 total points
ID: 34166579
Hi,

As you said its PK so if other table relates to this table then you need to check integrity of FK.

either you need to update id of FK table.

or need to change cascade relations.
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:colonelblue
ID: 34169298
Thank you both. If you don't mind me asking since I am more reliant on using the GUI ( For example using the diagrams, dropdown boxes, clicks ) because I am a newb and wouldn't even know where to run that code for a single row, is there a way to do it using the interface?
0
 
LVL 19

Assisted Solution

by:Bhavesh Shah
Bhavesh Shah earned 375 total points
ID: 34169872
Hi.

Just paste one of the code in query window.

Give table name where i write ur tablename.

Select database from database list which u want to use.

If you wanted to do via GUI then open ur table in design mode,first set auto identity to No for id column.

2nd save the table

3rd again set yes to auto identity column.

I knw u r new to sql but try 2 do,if u stuck,we are here to help u.

I suggest go with 1st option.
0
 

Author Closing Comment

by:colonelblue
ID: 34170482
Thank you especially Brichsoft.
You guys are awesome.
0
 
LVL 22

Expert Comment

by:dportas
ID: 34171712
Only use the GUI in this way if you have a backup or don't mind losing the data. Also make sure there is no-one else using the database. Using the GUI to modify tables like this can sometimes render the whole database ususable.

I strongly recommend you learn to use T-SQL so that you can write and execute SQL statements properly - that way you will have much more control over what you are doing.

IDENTITY columns have the irritating "feature" that they don't permit updates on the column. The easiest way to get around this may be to delete the rows and reinsert them.

The new SQL Server 2011 supports sequences, which makes it easier to create a surrogate key that you can update.
0
 
LVL 22

Expert Comment

by:dportas
ID: 34171719
Correction: "ususable" = "unusable"
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
configure service broker on all databases 2 94
Need help in debugging a UDF results 7 52
Database maintenance 36 101
Can I skip a node in XML? 9 32
There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.

749 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