I’ve had some bad experiences with cursors causing deadlocks. I have been trying to eliminate cursors wherever possible. When they are unavoidable, I try minimize the amount of work done while the cursor is open.
Is it ever better to copy the data to a table variable, and build a cursor over the table variable?
I know the answer will likely depend on the size of the data set. At them moment, I am concerned with a very small, data set (a handlefull of records), that is rarely updated. On occasion I will still have create complex loops over large data sets.
Is there any benefit to copying to table variables, or am I over-complicating things?
If you define the cursor as
STATICwhen you declare it, SQL Server will make a temporary copy of the data for you automatically.From the documentation: