Solved

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

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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
The viewer will learn how to count occurrences of each item in an array.

724 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