Home Forums SQL Server 2008 T-SQL (SS2K8) Repopulating a sequenced column that has gone out of sequence. RE: Repopulating a sequenced column that has gone out of sequence.

  • Thanks. Picture speaks a thousand words. So

    --Example table

    create table Labels

    (Id int identity (1,1),

    Value varchar(20),

    IntSequence bigint,

    ExternalSequence bigint,

    BatchId int)

    -- Insert some data

    Insert into labels

    (Value, IntSequence, ExternalSequence, BatchId)

    Values

    --batch 1

    ('exampleValue', 1,1,1),

    ('exampleValue', 2,2,1),

    ('exampleValue', 3,3,1),

    --batch 2

    ('exampleValue', 4,6,2),

    ('exampleValue', 5,7,2),

    ('exampleValue', 6,8,2),

    -- batch 3

    ('exampleValue', 7,10,3),

    ('exampleValue', 8,11,3),

    -- Insert the missing values for batch 1

    ('exampleValue', 9,4,1),

    ('exampleValue', 10,5,1),

    -- insert the missing values for batch 2

    ('exampleValue', 11,9,2)

    select * from Labels -- Current order showing external seq in wrong order, but internal seq in correct order.

    Select * from labels order by ExternalSequence asc -- Order by the other column which is in correct order shows true order, but internal sequence in wrong order

    Hopefully the above code helps. I need to get the internal sequence to be in the sequential order dependant on the external sequence. My real problem is like the above, but unfortunately I cannot just copy the external sequence to internal!

    I was playing around with using something like

    declare @id int

    set @id = 0

    update labels

    set @id = intsequence = @id + 1

    but not sure on how to update it in sort order?