I have two tables where TableA has latest data and TableB has some old data. I want to update TableB's data if it matches id with TableA and if doesn't match insert new row in TableB.
I got a solution from stackOverflow
begin tran
if exists (select * from t with (updlock,serializable) where pk = @id)
begin
update t set hitCount = hitCount+1
where pk = @id
end
else
begin
insert t (pk, hitCount)
values (@id, 1)
end
commit tran
But it seems I need to pass @id each time, may be I am not getting it in the correct way. I have hundreds of row to update/insert from tableA.