dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL
Expertise: Intermediate
Apr 4, 2009

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


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.

Srinath MS
 
Thanks for your registration, follow us on our social networks to keep up-to-date