CREATE TABLE SEQ (COK NVARCHAR(10)) insert into seq values ('York'),('Boston'),('Chicago') select * from seq --step 1, I create a table and populate it with three rows now I count the rows select count(*) from seq --and start my sequence from the rowcount+1 create sequence myseq as int start with 4 increment by 1 --I add a new column alter table seq add conta int --I add a constraint alter table seq add constraint addseq default (next value for myseq) for [conta] --Insert a new row Insert into seq values ('Seattle',default) --Select again select * from seq --as you can see we have three rows with null value. They are the rows inserted before the constraint declare @co int select @co=count(*) from seq where conta is null declare @inc int set @inc=1 while (@inc<=@co) begin declare @city as nvarchar(10) select top 1 @city=cok from seq where conta is null update seq set conta=@inc where cok=@city set @inc=@inc+1 end --now all the rows follow a sequence
When the table has a Clustered Index on COK column, the TOP1 behaves differently and the sequence values assigned will be different.
Try creating an index just before running the while loop and check the difference.
create clustered index IX_SEQ on SEQ(COK asc)