If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Identity

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-31-10, 09:10
shantanu4u shantanu4u is offline
Registered User
 
Join Date: Feb 2010
Posts: 9
Identity

create table table1 (col1 int identity(1,1),col2 varchar(10))

i created the table like this
now i want to alter col1 which can increase by value 2


something like that

alter table table1 alter column col1 int identity(1,2)

plz help

i have done this through wizard

i want sql query

Last edited by shantanu4u; 08-31-10 at 09:22. Reason: solution
Reply With Quote
  #2 (permalink)  
Old 08-31-10, 09:20
MCrowley MCrowley is offline
Wage drone 24601
 
Join Date: Jan 2003
Location: Massachusetts
Posts: 4,899
If the table is not large, the easiest thing to do would be to create a new table with the desired identity column, then transfer over the data from the old table to the new table. sp_rename will complete the process.
Reply With Quote
  #3 (permalink)  
Old 08-31-10, 09:24
shantanu4u shantanu4u is offline
Registered User
 
Join Date: Feb 2010
Posts: 9
ya you are right but is there any way to alter the identity column
Reply With Quote
  #4 (permalink)  
Old 08-31-10, 13:45
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
If you are updating an identity value, you are doing something dreadfully wrong in the first place.
Why would you want to do this?
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #5 (permalink)  
Old 08-31-10, 13:54
MCrowley MCrowley is offline
Wage drone 24601
 
Join Date: Jan 2003
Location: Massachusetts
Posts: 4,899
Another way to do it:
Code:
create table test1 
(col1 int identity (1, 1) not null,
 col2 varchar(20),
 col3 varchar(30),
 constraint PK_Test1 primary key (col1))
go
insert into test1 (col2, col3) values ('hello', 'good bye')
insert into test1 (col2, col3) values ('hi', 'bye')
select *
from test1

go
alter table test1 drop constraint PK_test1
alter table test1 drop column col1
alter table test1 add col1 int identity (1, 2)
go
select *
from test1
go
drop table test1
Reply With Quote
  #6 (permalink)  
Old 09-01-10, 13:53
apurgert apurgert is offline
Registered User
 
Join Date: Jul 2009
Posts: 47
If I'm not mistaken you could always just drop the column then add it with a reseed.

IE

Code:
ALTER TABLE table1
DROP COLUMN col1
Then

Code:
ALTER TABLE table1
ADD col1 int IDENTITY(1,2)
Why do you need it to increment by two anyways?
Reply With Quote
  #7 (permalink)  
Old 09-01-10, 13:56
MCrowley MCrowley is offline
Wage drone 24601
 
Join Date: Jan 2003
Location: Massachusetts
Posts: 4,899
Most common reason would be for some sort of merge operation, when you want to make sure that two copies of the table can not conflict with each others' primary keys. Less common is a general bias against even numbers.
Reply With Quote
  #8 (permalink)  
Old 09-01-10, 14:12
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,602
As blindman pointed out early in this thread, knowing what the values of an identity column is a mistake from the outset and caring what those values are is even worse. This is a disaster searching for its next victim, do NOT bellyflop into the maw!!!

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On