MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 2nd, 2004, 03:36 AM
djavet djavet is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 31 djavet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 59 m
Reputation Power: 5
Question renumber an ID column

Hello,

I've a ID column like this:

IDcol | Name
273 | namexxx
274 | name xxx
276 | name1
277 | name2
278 | name3
279 | name4

279 | name5

I wish to rename the IDcol filed with -1 to have this:
IDcol | Name
273 | namexxx
274 | name xxx
275 | name1
276 | name2
277 | name3
278 | name4

279 | name5

How can I do that into MSSQL?

I appreciate your help and time.
Regards, Dom

Reply With Quote
  #2  
Old August 2nd, 2004, 04:06 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,784 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 21 h 42 m 49 sec
Reputation Power: 37
Why? The only purpose of a identity column is to provide unique values.

Reply With Quote
  #3  
Old August 2nd, 2004, 04:46 AM
djavet djavet is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 31 djavet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 59 m
Reputation Power: 5
Correct, but this is not unique, it's a id from a region table.
This nothing to do with auto-incremant.
My exemple is only a extract of my table.

Regards, Dom

Reply With Quote
  #4  
Old August 2nd, 2004, 06:30 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,784 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 21 h 42 m 49 sec
Reputation Power: 37
Ok, I see.

Do you want to get rid of the duplicate id or do you want to fill up all gaps? I.e. if it's only for those specific names shown in the sample, it is easy,

Code:
update t set id = id - 1 where name in ('name1','name2','name3','name4')


Otherwise it would probably be easiest to use a cursor (assuming that this is a one time operation).

Reply With Quote
  #5  
Old August 2nd, 2004, 06:45 AM
djavet djavet is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 31 djavet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 59 m
Reputation Power: 5
Cool query. I se, but I've more than 200 lines to renumered (-1)...

Any idea?

Dom

Reply With Quote
  #6  
Old August 2nd, 2004, 12:22 PM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,784 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 21 h 42 m 49 sec
Reputation Power: 37
No idea for doing with one query since I can't discern any pattern.

Reply With Quote
  #7  
Old August 2nd, 2004, 02:16 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,982 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 18 h 20 m 26 sec
Reputation Power: 1028
i can't see any pattern either

however, if it's a one-time operation, use a text editor

pull out the 200 entries that need to be renumbered

(how you know which ones they are is the problem, isn't it?)

then just copy/paste them into the code that swampboogie gave in post #4

that would indeed be the fastest way

(i speak from experience, having done similar things many times)
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > renumber an ID column


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT