Solved

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

Posted on 2012-03-29
4
283 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 24

Accepted Solution

by:
DBAduck - Ben Miller earned 500 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 24

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn how to dynamically set the form action using jQuery.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now