Hi Hanno,
I understand what you're trying to achieve. Welcome to the world of a variety of databases!
SQL server 2000 supports temporary tables created by prefixing a # to the table name, making it a locally accessible temporary table (local to the session) and preceding ## to the table name, for globally accessible temporary tables e.g #MyLocalTable and ##MyGlobalTable respectively.
SQL server 2005 and above support both temporary tables (local, global) and table variables - watch out for new functionality on table variables in SQL 2008 and release two!
The difference between temporary tables and table variables is not so big but lies in the the way the database server handles them.
I would not wish to talk about older versions of SQL server like 7, 6, though I have worked with them and it's where I came from anyway :-)
It’s common to think that table variables always reside in memory but this is wrong. Depending on memory usage and the database server volume of transactions, a table variable's pages may be exported from memory and get written in tempdb and the rest of the processing takes place there (in tempdb).
Please note that tempdb is a database on an instance with no permanent objects in nature but it’s responsible for handling workloads involving side transactions like sorting, and other processing work which is temporary in nature.
On the other hand, table variables (usually with smaller data) are kept in memory (RAM) making them faster to access and therefore less disk IO in terms of using the tempdb drive when using table variables with smaller data compared to temporary tables which always log in tempdb.
Table variables cannot be indexed while temporary tables (both local and global) can be indexed for faster processing in case the amount of data is large. So you know your choice in case of faster processing with larger data volumes by temporary transactions.
It's also worth noting that transactions on table variables alone are not logged and can't be rolled back while those done on temporary tables can be rolled back!
In summary, table variables are better for smaller data while temporary tables are better for larger data being processed temporarily.
If you also want proper transaction control using transaction blocks, table variables are not an option for rolling back transactions so you're better off with temporary tables in this case.
Lastly, temporary tables will always increase disk IO since they always use tempdb while table variables may not increase it, depending on the memory stress levels.
Let me know if you want tips on how to tune your tempdb to earn much faster performance to go above 100%!
God bless!
Regards,
Chris Musasizi
Lead SQL DBA at MTN, Author & Expert at SQL Server Central & Experts Exchange respectively.
About Chris:
Chris is the Lead DBA at MTN, Uganda's leading mobile telecom which is part of MTN group, the sponsors of the World cup 2009. Chris works on Business Systems that demand close to 100% uptime in a year. Over the years, Chris has worked in the communications industry majorly with good background in bridging between database technology and business strategies to provide highly available, stable and optimised business solutions.
Chris has majorly worked with Oracle, MSSQL, Informix and MySQL database systems and storage engines, NetApp NAS and SAN storage systems. Chris primarily being certified Software engineer, started career as a developer (both web and distributed systems), then did Business Intelligence (BI), later did enterprise work, DBA, and finally managerial work.
Prior to MTN, he was the Senior IT Manager at Posta Uganda, the leading postal and Courier Company in the country.
Feel free to mail: [email protected]
Have you read John 3:3, John 3:16? Knowledge is power!
Together, let's work towards Heaven!