Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

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
?
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Simplify Your Workload with One Tool

How do you combat today’s intelligent hacker while managing multiple domains and platforms? By simplifying your workload with one tool. With Lunarpages hosting through Plesk Onyx, you can:

Automate SSL generation and installation with two clicks
Experience total server control

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…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
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.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

618 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