Skip to content

SQL 201-Table Variables

2011 August 3
tags:
by Shannon Lowder

It’s been a while since I’ve added to my SQL 201 series, so I thought I’d pick up with table variables.  In an earlier post, I covered temp tables. Temp tables are a great place to dump in raw data, then transform it into another form so you can use it in a different way.  Table variables are similar.  You can dump in, transform it, and then pull it back out, all without having to worry about cleanup when you’re done.

There are a few questions I ask when determining when to use a table variable over a temp table:

  • Do you need the data to persist when you’re done with it?
  • Do you want to keep the data in memory?
  • You aren’t going to need an index are you?
  • You aren’t going to try to do an SELECT … INTO with the table, right?

As long as you don’t care about persisting the data, you want to keep the data in memory (and in theory as fast as possible) and you don’t want to index the data or SELECT data INTO the table, then I usually go for table variables.

So how do I define a table variable?

The syntax is somewhere between a table declaration and a variable declaration.

   1: DECLARE @<tableName> TABLE (

   2:     <columnName> <datatype>

   3:     , <columnName> <datatype>

   4:     ...

   5: )

You have to define the table variable name, then define the columns just like you would a normal table.  It’s really that easy.

Seems easy, what’s the catch?

Well, it’s not all sunshine.  There are some catches to using table variables.

  • You can’t truncate a table variable.  You have to start a new code segment and declare the new version of the table variable, or create a new table variable in order to change the columns in any way.
  • If you’re starting to use dynamic SQL, You aren’t going to be able to create the table in dynamic code, then have access to it outside the dynamic part of your query.  There’s a change of scope there.
  • SQL can’t generate statistics on a table variable, if you look at the estimated number of rows in an execution plan for a query that contains a table variable, you’ll see it estimates 1 row.  If you’re going to deal with tens of thousands of rows, table variables are not for you.  You need to consider indexing, which requires temp tables or regular tables.
  • If you need a nested stored procedure to reference the data in a table variable declared in the parent procedure, you’ll have to pass that variable to the child procedure.  Again, the scope changes when you enter the sub-procedure.

Conclusions

You have to understand the pros and cons for all three table types: real tables, temp tables, and table variables.  Each one has it’s use, and each one has cases where you can’t use them.  That’s why there are three.  Learning when you can and can’t use each of them is what you’ll have to learn to master SQL. 

When you come to a situation where you could use one or more of these tables, try two.  See which one performs better.  Testing and experimentation will teach you more than this article ever could.

If you have any questions, please send them in.  I’m here to help!

3 Responses leave one →
  1. September 21, 2011

    Hi Shannon,

    Nice article, and I definitively agree with you that, in general, table variables should not be used on large table (in fact I just blogged about this).

    I found two myths in your post, the first being that table variable can’t have index. It is very true that you can’t *add* indexes but you can create indexes on a table variable by specifying a clustered primary key or unique constraint.

    Also, table variable are not necessary a *in memory* structure, they are created in tempdb and can be swapped to disk under memory pressure. Note that temporary tables can also be cached in memory.

    See my post on table variable for large table and tell me what you think: http://www.sqlbadpractices.com/using-table-variable-for-large-table-vs-temporary-table/

    Francois.

    • September 22, 2011

      I hadn’t considered clustered primary keys or unique constraints when I was talking about indexing on a table variable. I had meant you couldn’t do custom indexing. Let’s say you had adventureworks.person.contacts stored in a table variable. I couldn’t create a covering index on ContactID, FirstName, LastName. There are times when I’m building a table variable (or other temporary structure where I need custom indexing, and when I do, I end up moving to a temp table or other structure to get the functionality I’m looking for.

      I’ll also concede the fact that temporary structures are stored in tempdb and may not neccessarily exist in memory. But, when it temp structures are written to disc it’s not the standard IO hit. I do need to do more reading on exactly how the table variable or temp table are written to tempdb, but I understand it’s not the standard write data, log data approach. It’s a type of lazy writer if I’m not mistaken.

      I appreciate you taking the time to read and coment on the article! I think you may have prompted me to write about temporary structures in a deeper dive. I’m preparing for the MCM exams and I want to know these structures as deeply as I can to prepare for that exam!

Trackbacks and Pingbacks

  1. SQL201-The Syllabus | Shannon Lowder

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS