Login | Register   
Twitter
RSS Feed
Download our iPhone app
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 3, 2009

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
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap