what are the main purposes for which temporary tables are used?
To go along with what Gail said above...
When used properly do "Divide'n'Conquer" a large "problem", you can get absolutely mind boggling blinding speed out of some queries. Seriously. Way too many people think that "good set based code" has to be a single query to accomplish a task and nothing could be further from the truth. I've used Temp Tables to divide up queries that have dozens of joins (typically created by an ORM or someone using a query designer) and that previous took anywhere from 45 minutes to 2 hours to run and have gotten them to run in seconds.
There are DBA's that won't allow Temp Tables to be used by developed code. It sometimes takes a bit to convince them that T-SQL will build "Work" tables in Temp DB behind the scenes but they normally come around when you show them that properly used Temp Tables can convert long running, resource greedy code that takes (sometimes) hours to run into something that takes only 3 or 4 seconds to run.
is pronounced ree-bar and is a Modenism for R
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair
How to post code problemsHow to post performance problemsForum FAQs