I have thousands of rows in a sql server table which have a START row, I need to update the table and INSERT an END row for each of them.
select distinct transaction_id from transactions t1
where
this_status_id = 2 and that_type_id = 1
and not exists
(
select *
from transactions t2
where t2.transaction_id = t1.transaction_id
and t2.this_status_id in (1,3,4)
and t2.that_type_id = 1
)
This select returns a list of IDs (not the primary key) I need to loop through each of the ids from the above select, and INSERT into the same table like:
INSERT INTO transactions VALUES (@transaction_id, "finished", 1, 2, GETDATE())
INSERT into transactions SELECT distinct transaction_ID, 'finished',1,2,getdate() FROM transactionsfrom MSFT (Insert into select...syntax. Loops are slow... set based processing in a RDBMS is so much more efficient.