devxlogo

Table Variables Are More Efficient than Temporary Tables In Some Cases

Table Variables Are More Efficient than Temporary Tables In Some Cases

SQL developers are mostly familiar with using temporary tables to temporarily accumulate or store query results. However, using a “table variable,” a less well-known concept, turns out to be a better performer in some cases.

Specifically, when you have only a few rows that you need to hold in a table, and you don’t need to index the data, use a table variable instead of a temporary table. The table variable performs better because SQL Server doesn’t create statistics on a table variable as it does for temporary tables.

Here’s the syntax to create a table variable:

DECLARE @RESULTS TABLE(   FACTOR_ID INT,   WEIGHTING DECIMAL(5,2),   HOLDER_CTG_ID INT)

Remember, if you need to create indexes on your temporary data, use a temporary table instead.

devx-admin

Share the Post: