• Hi

    So i am assuming that your new table contains the id and the max time of the corresponding id in the original table.

    The problem with the while loop that your are using is that nothing is updating your @id.

    I will suggest not to do a while loop at all since this will in effect look at one id record at a time, rather do it in bulk.

    I suggest something like this.

    -- Update the records you have

    update New

    setnew.time = max(orig.time)

    from Testdb.dbo.new_table newwith (nolock)

    join Testdb.dbo.orig_table origwith (nolock)

    on orig.id = new.id

    -- insert those that you don't have

    insert into Testdb.dbo.new_table with (tablock)

    select orig.id,max(orig.time)

    from Testdb.dbo.orig_tableorig with (nolock)

    left join Testdb.dbo.new_table newwith (nolock)

    on orig.id = new.id

    where new.id is

    group by orig.id

    You can put this code into a stored proc as well if needed.

    Please inform if this above code works for you.

    --------------------------------------------------------------------------------------------------------------------------------------------------------
    To some it may look best but to others it might be the worst, but then again to some it might be the worst but to others the best.
    http://www.sql-sa.co.za