sql server 2008 r2 - how to update an identity column values -
this question has answer here:
id employeename address phoneno emailid dob doj designation department craeteddate 2 sabarish saidapet 9600387983 sabari@gmail.com -1973 -2012 ase software 2013-05-15 17:07:43.223 10 karthik chrompet 9865028330 karthik@gmail.com -1968 -2008 softwareengineeer software 2013-05-15 15:40:41.613 15 sabarish saidapet 9600387983 sabari@gmail.com -1973 -2012 ase software 2013-05-15 17:07:12.003 in above table id column identity column , want id values 1,2,3, instead of 2,10,15 how this?
try 1 -
query:
if object_id (n'dbo.test') not null drop table dbo.test create table dbo.test ( id int not null identity(1,1) primary key , employeename varchar(50) not null ) set identity_insert dbo.test on insert dbo.test ( id , employeename ) values (2, 'sabarish saidapet'), (10, 'karthik chrompet'), (15, 'sabarish saidapet') set identity_insert dbo.test off set identity_insert dbo.test on declare @temp table ( id int not null , employeename varchar(50) not null ) insert @temp (employeename, id) select t.employeename , row_number() on (order (select 1)) dbo.test t truncate table dbo.test --delete dbo.test insert dbo.test (id, employeename) select id, employeename @temp set identity_insert dbo.test off insert dbo.test (employeename) values ('test 4') select * dbo.test t output:
id employeename ----------- -------------------------------------------------- 1 sabarish saidapet 2 karthik chrompet 3 sabarish saidapet 4 test 4
Comments
Post a Comment