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!