There are several problems with the code, but the main one is the update operator, which updates several records in the database at once. For example, when the loop starts the first update, it updates four records, where inst_id = '1' and const_code = 'PU', adding 1 second to each of them. In the second iteration, he adds 2 seconds for all four entries in the third iteration, and finally 4 seconds for all four entries in the fourth iteration (making all of them 1 + 2 + 3 + 4 = 10 seconds after 5:00).
The best solution is to add a new column, an identifier that is of type INT IDENITY PRIMARY KEY and discards the composite primary key in the inst_id, cons_code and eff_date_time columns.
However, if for some reason you should use a composite primary key in these three fields, then here is the code for this.
NOTE. You cannot just add a unique number to each date, because you can get to the point where you have made so many second increments that they actually come across a different value that you find in the table. This is the reason for the EXISTS code part.
drop table [constants_temp] go drop table [constants_new] go create table [constants_temp] ( inst_id varchar(25), cons_code varchar(10), eff_date_time datetime ) insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('1', 'PU', '1901-01-01 17:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('1', 'PU', '1901-01-01 17:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('1', 'PU', '1901-01-01 17:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('1', 'PU', '1901-01-01 17:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('2', 'PU', '1901-01-01 17:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('2', 'PU', '1901-01-01 17:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('2', 'PU', '1901-01-01 17:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('2', 'PU', '1901-01-01 17:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('3', 'PU', '1901-01-01 17:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('3', 'PU', '1901-01-01 17:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('3', 'PU', '1901-01-01 17:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('3', 'PU', '1901-01-01 17:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('4', 'PU', '1901-01-01 17:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('4', 'PU', '1901-01-01 17:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('4', 'PU', '1901-01-01 17:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('4', 'PU', '1901-01-01 17:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('A', 'PU', '1901-01-01 17:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('A', 'PU', '1901-01-01 17:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('A', 'PU', '1901-01-01 17:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('A', 'PU', '1901-01-01 17:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('A-1', 'AS', '1972-07-01 00:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('A-1', 'AS', '1972-07-01 00:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('A-1', 'AS', '1972-07-01 00:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('A-10', 'AS', '1972-07-01 00:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('A-10', 'AS', '1972-07-01 00:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('A-10', 'AS', '1972-07-01 00:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('A-11', 'AS', '1972-07-01 00:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('A-11', 'AS', '1972-07-01 00:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('A-11', 'AS', '1972-07-01 00:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('A-12', 'AS', '1972-07-01 00:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('A-12', 'AS', '1972-07-01 00:00:00.000') insert into [constants_temp] (inst_id, cons_code, eff_date_time) values ('A-12', 'AS', '1972-07-01 00:00:00.000')