Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 44421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:38:58+00:00 2026-05-10T15:38:58+00:00

In SQL Server 2005, we can create similar tables in two different ways. We

  • 0

In SQL Server 2005, we can create similar tables in two different ways.

We can use a table variable:

declare @tmp table (Col1 int, Col2 int); 

Or we can use a temporary table:

create table #tmp (Col1 int, Col2 int); 

What are the differences between these two? I have read conflicting opinions on whether @tmp still uses tempdb, or if everything happens in memory.

In which scenarios does one outperform the other?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T15:38:59+00:00Added an answer on May 10, 2026 at 3:38 pm

    There are a few differences between Temporary Tables (#tmp) and Table Variables (@tmp), although using tempdb isn’t one of them, as spelt out in the MSDN link below.

    As a rule of thumb, for small to medium volumes of data and simple usage scenarios you should use table variables. (This is an overly broad guideline with of course lots of exceptions – see below and following articles.)

    Some points to consider when choosing between them:

    • Temporary Tables are real tables so you can do things like CREATE INDEXes, etc. If you have large amounts of data for which accessing by index will be faster then temporary tables are a good option.

    • Table variables can have indexes by using PRIMARY KEY or UNIQUE constraints. (If you want a non-unique index just include the primary key column as the last column in the unique constraint. If you don’t have a unique column, you can use an identity column.) SQL 2014 has non-unique indexes too.

    • Table variables don’t participate in transactions and SELECTs are implicitly with NOLOCK. The transaction behaviour can be very helpful, for instance if you want to ROLLBACK midway through a procedure then table variables populated during that transaction will still be populated!

    • Temp tables might result in stored procedures being recompiled, perhaps often. Table variables will not.

    • You can create a temp table using SELECT INTO, which can be quicker to write (good for ad-hoc querying) and may allow you to deal with changing datatypes over time, since you don’t need to define your temp table structure upfront.

    • You can pass table variables back from functions, enabling you to encapsulate and reuse logic much easier (eg make a function to split a string into a table of values on some arbitrary delimiter).

    • Using Table Variables within user-defined functions enables those functions to be used more widely (see CREATE FUNCTION documentation for details). If you’re writing a function you should use table variables over temp tables unless there’s a compelling need otherwise.

    • Both table variables and temp tables are stored in tempdb. But table variables (since 2005) default to the collation of the current database versus temp tables which take the default collation of tempdb (ref). This means you should be aware of collation issues if using temp tables and your db collation is different to tempdb’s, causing problems if you want to compare data in the temp table with data in your database.

    • Global Temp Tables (##tmp) are another type of temp table available to all sessions and users.

    Some further reading:

    • Martin Smith’s great answer on dba.stackexchange.com

    • MSDN FAQ on difference between the two: https://support.microsoft.com/en-gb/kb/305977

    • MDSN blog article: https://learn.microsoft.com/archive/blogs/sqlserverstorageengine/tempdb-table-variable-vs-local-temporary-table

    • Article: https://searchsqlserver.techtarget.com/tip/Temporary-tables-in-SQL-Server-vs-table-variables

    • Unexpected behaviors and performance implications of temp tables and temp variables: Paul White on SQLblog.com

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 68k
  • Answers 68k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Unfortunately, no you cannot infer anything from 4xx or 5xx… May 11, 2026 at 12:14 pm
  • added an answer Unfortunately no. The problem is thus; A jpeg file consists… May 11, 2026 at 12:14 pm
  • added an answer Write to the webservice provider. Tell them about the malformed… May 11, 2026 at 12:14 pm

Related Questions

In SQL Server 2005, we can create similar tables in two different ways. We
We have a Single Statement FUNCTION in SQL Server 2005 which uses CONTAINSTABLE(). All
We are utilizing the XML data type in Microsoft SQL Server 2005 for a
We're using SQL Server 2005 in a project. The users of the system have
In SQL Server 2005 I have an id field in a table that has
In SQL Server 2005, is there a way of deleting rows and being told
In SQL Server 2005, are there any disadvantages to making all character fields nvarchar(MAX)
In SQL Server 2005, is there a way for a trigger to find out
In SQL server 2005 this query select len(cast('the quick brown fox jumped over the
In SQL Server 2005 Management Studio, it appears that the output of PRINT statements

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.