Method 1: Using a cursor declare cursor1 cursor local for select * from tablename open cursor1 fetch next from cursor1 into #temptable while @@fetch_status = 0 begin if exists (select column from tablename where id = (select id from #temptable)) begin  /* do stuff here */  end else begin /* do other stuff here */ [...]