Cloudy Move offers everything you need to build your custom web application. Create product inventory, send and receive payments. Sell your products and let your customers earn cashbacks and rewards. Choose any design templates, add and modify your contents and posts and be found everywhere. Sign up for free.
9/9/2021 10:08:41 AM
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 SELECT
s 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://docs.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
Committed to grow your business like never before. We offer best digital consultancy services at affordable cost.
We tailor to your needs whatever they may be. Navigating your business online couldn’t get easier.
With us your business doesn't have to social distance online. Connect with people no matter where they are.
Not sure what you need? Scroll through our blog for advice, or send us a message!
There are many options when it comes to using digital tools, and technology is always changing.
Earn 1000 DreamozTech tokens when you sign up, earn extra 500 on referral.
Our SEO platform allows you to be creative, while still helping you boost your SEO ranking.
Our web tools are now at your fingertips. With our new mobile app manage your online business at the comfort of your phone.
Search engines allow people to look for the product or service they want, at the precise time they want it.
Build your responsive, mobile friendly, dynamic and search engine optimized web apps. Update and manage your contents instantly. Bring more traffic, use our tools and thrive in the digital world. By creating your free profile, you are one step closer to get the best results. Our SEO platform allows you to be creative, while still helping you boost your SEO ranking.
It's never been easier, cheaper or more beneficial for your business to get online. Don’t be intimidated by the breadth of opportunities in digital.
Websites and social media can be used to reach existing and potential customers
Answers to most common questions
By creating your free profile, you are one step closer to get the best results. Sign up now!
No, Do you know the basic idea of price hike for various brands in shopping malls?
Internet banking and payment options. Compare prices with brand and see the difference.
We provide you with solutions for making engaging web apps, help in mobile support and much more!
We can be the early bird you've always wanted to be without your alarm clock.
We would be glad to help you with your further queries.
What's new at Cloudy Move