Temp Tables, Table Variables, and CTEs

Published On: 2007-12-10By:

There are some major differences between temp tables, table variables and common table expressions (CTEs).  Some of the big differences are:

Temp Tables vs. Table Variables

  1. SQL Server does not place locks on table variables when the table variables are used.
  2. Temp tables allow for multiple indexes to be created
  3. Table variables allow a single index the Primary Key to be created when the table variable is declared only.  There is an exception to this, that if you can create the index inline, for example by creating a unique constraint inline as shown in the comments.  However these indexes (and the table variable in general) will always be assumed to have 1 row in them, no matter how much data is within the table variable.
  4. Temp tables can be created locally (#TableName) or globally (##TableName)
  5. Table variables are destroyed as the batch is completed.
  6. Temp tables can be used throughout multiple batches.
  7. Temp tables can be used to hold the output of a stored procedure (temp tables will get this functionality in SQL Server 2008).

Table variables and Temp Tables vs. CTEs

  1. CTEs are used after the command which creates them.
  2. CTEs can be recursive within a single command (be careful because they can cause an infinite loop).
  3. Table variables and Temp Tables can be used throughout the batch.
  4. The command before the CTE must end with a semi-colon (;).
  5. As Temp tables and table variables are tables you can insert, update and delete the data within the table.
  6. CTEs can not have any indexes created on them, source tables much have indexes created on them.

If you can think of anything that I’ve missed, feel free to post them in the comments.


A well timed code freeze is critical to keeping your sanity.

Published On: 2007-12-05By:

Doing a code freeze at this time of year can be crucial to keeping your sanity this time of year.  Often times the business unit likes to push out last minute changes before the year end.  This often means pushing last minute code (which as everyone knows isn’t always perfect) which can lead to unpleasant little phone calls from the business or the NOC in the middle of holiday festivities.

Do your self and your family a favor, push for a code freeze until after the new year.  The developers will thank you, the business with hate you (but really what else is new).  I like about a three week code freeze (emergency bug fixes are except, but they have to actually be bug fixes, and they have to be an emergency) starting about December 15 ending about January 3 (depending on when the weekends fall).

It can be tough to get a code freeze implemented at first.  But after the first year with the code freeze everyone will want to go it the next year.


SQL 2008 November CTP

Published On: 2007-12-03By:

We’ll I’ve finely gotten around to installing the SQL 2008 November CTP.  While walking through the installer I have seen some excellent changes.  Not only is the option to change the paths of the data files no longer hidden, there are more than just two options.  There are at least 7 paths that you get to specify while installing.  The first one is the Shared component directory.  This appears to be the base path where all your binarys will be based off of.

 Next you get the Instance root directory.  This is where the system database files will be based off of, so make sure that you don’t point this to the C drive is you want the system databases on another drive.

A couple of screens later you get to set 6 install paths.  The first is the data root directory.  This changes the base path for all the others options.  The others are the User database folder, user log database folder, tempdb data folder, tempdb log folder, and the backup directory.

This amount of flexability in the installer is a first, and it’s a welcome change.  Look for more posts, tips and articles about SQL Server 2008 in the comming weeks and months up till the release.


New Article: Tricks to increase SQL Server query performance

Published On: 2007-11-30By:
1 318 319 320 321 322 325


Globally Recognized Expertise

As Microsoft MVP’s and Partners as well as VMware experts, we are summoned by companies all over the world to fine-tune and problem-solve the most difficult architecture, infrastructure and network challenges.

And sometimes we’re asked to share what we did, at events like Microsoft’s PASS Summit 2015.