[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

What are the differences between a table variable, a temp table and a derived table?

Posted on 2012-03-29
4
Medium Priority
?
293 Views
Last Modified: 2013-11-19
Hi ,

I would like to know  differences between a table variable, a temp table and a derived table?
0
Comment
Question by:SaiSiva08
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
DBAduck - Ben Miller earned 1500 total points
ID: 37785559
A table variable is a variable that takes the form of a table.  In most cases it is backed by a Work Table in tempdb, but it is in the form of an @ variable.

DECLARE @t1 TABLE (col1 int NOT NULL)

INSERT INTO @t1 (col1)
VALUES (34)

A temp table is a materialized table that you create with the same syntax as you do a concrete table, but it has a prefix of # or ##.
# prefix is a session based temp table that is destroyed by a cleanup process after the session goes out of scope and a ## prefix is a global temp table that gets destroyed after the last session references it.

CREATE TABLE #t1 (col1 int NOT NULL)
INSERT INTO #t1 (col1)
VALUES (34)

or the above with ##t1

A derived table is a term that I don't hear much but you can think of them as tables that are derived from a query.  I call them Virtual tables, but here is an example.

SELECT *
FROM (SELECT * FROM dbo.t1) A

Table A is derived from a query and only lives for the duration of the query.  Some people call them subquery tables, or other names, but this is a derived table.  Today rather than use a derived table, we use CTEs (Common Table Expressions) instead.

WITH T1 (Col1)
AS (
   SELECT col1
   FROM dbo.t1
)
SELECT *
FROM T1
0
 
LVL 25

Expert Comment

by:TempDBA
ID: 37785611
A table variable just works like a static variable i.e. it scope lies within a certain block of execution whereas temp tables has a scope for the session unless it is dropped.
Table variables are recommended for holding very less data i.e. (10 KB) approx as it doesn't have statisctics build on it. So, it makes the performance of the query to be slow whereas temp tables are far beyond that. You can make its use whereever and whenever required but  its over usage creates problem with SGAM of tempdb and you will start seeing problem as 2:1:103.
0
 
LVL 25

Expert Comment

by:DBAduck - Ben Miller
ID: 37804265
Any update on this question?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37804531
As a brand new member, I suspect she has not got the "hang of things".  In other words, she does not realize that appropriate feedback can enhance or re-focus the question.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question