SQL server temporary tables vs cursors

SARAVAN picture SARAVAN · Dec 31, 2010 · Viewed 31.1k times · Source

In SQL Server stored procedures when to use temporary tables and when to use cursors. which is the best option performance wise?

Answer

marc_s picture marc_s · Dec 31, 2010

If ever possible avoid cursors like the plague. SQL Server is set-based - anything you need to do in an RBAR (row-by-agonizing-row) fashion will be slow, sluggish and goes against the basic principles of how SQL works.

Your question is very vague - based on that information, we cannot really tell what you're trying to do. But the main recommendation remains: whenever possible (and it's possible in the vast majority of cases), use set-based operations - SELECT, UPDATE, INSERT and joins - don't force your procedural thinking onto SQL Server - that's not the best way to go.

So if you can use set-based operations to fill and use your temporary tables, I would prefer that method over cursors every time.