Solved

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

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

919 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

18 Experts available now in Live!

Get 1:1 Help Now