We help IT Professionals succeed at work.

Writing a trigger to handle batch updates

purplesoup
purplesoup asked
on
Can someone just remind me the best way to write a trigger to handle batch updates, I know basically the inserted table could contain multiple rows, so you have to be able to loop through each row and handle it, but I just can't see an example from googling.

Thanks
Comment
Watch Question

You can do this either procedural or through set-based.  I don't know your details so set-based is too tough to describe.  Here's an example of doing it procedurally:


declare @field1 varchar(20), @field2 int

declare mycursor cursor for select field1, field2 from inserted
open mycursor
fetch next from mycursor into @field1, @field2 -- gets the current row's contents based on the query
while @@fetch_status=0
begin
     --do something to the row here
    fetch next from mycursor into @field1, @field2
end

close mycursor
deallocate mycursor