0% found this document useful (0 votes)
113 views2 pages

CTE, Temp Table, and Table Variable Explained

The document discusses the differences between common temporary SQL objects: CTEs, temporary tables, and table variables. CTEs are named result sets that exist for a single statement, temporary tables are stored in tempdb and can be used by multiple sessions, and table variables exist in memory or tempdb for a single batch and are only accessible by the current user.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views2 pages

CTE, Temp Table, and Table Variable Explained

The document discusses the differences between common temporary SQL objects: CTEs, temporary tables, and table variables. CTEs are named result sets that exist for a single statement, temporary tables are stored in tempdb and can be used by multiple sessions, and table variables exist in memory or tempdb for a single batch and are only accessible by the current user.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Difference between CTE , Temporary table and table variable

1. Temp Tables are physically created in the Tempdb database. These tables act as the
normal table and also can have constraints, index like normal tables.
2. CTE is a named temporary result set which is used to manipulate the complex sub-queries
data. This exists for the scope of statement. This is created in memory rather than
Tempdb database. You cannot create any index on CTE.
3. Table Variable acts like a variable and exists for a particular batch of query execution. It
gets dropped once it comes out of batch. This is also created in the Tempdb database but
not the memory.

Difference between Temporary Table and Table Variable

 A Temp table is easy to create and back up data. But the table variable involves the
effort when you usually create the normal tables.

 Temp table result can be used by multiple users. But the table variable can be used
by the current user only.

 Temp table will be stored in the tempdb. It will make network traffic. When you
have large data in the temp table then it has to work across the database. A
Performance issue will exist. But a table variable will store in the physical memory
for some of the data, then later when the size increases it will be moved to the
tempdb.

 Temp table can do all the DDL operations. It allows creating the indexes, dropping,
altering, etc.., where table variable won't allow doing the DDL operations. But the
table variable allows us to create the clustered index only.

 Temp table can be used for the current session or global. So that a multiple user
session can utilize the results in the table. But the table variable can be used up to
that program. (Stored procedure)

 Temp variable cannot use the transactions. When we do the DML operations with
the temp table then it can be rollback or commit the transactions. But we cannot do
it for table variable.

 Functions cannot use the temp variable. But the function allows us to use the table
variable. More over we cannot do the DML operation in the functions but using the
table variable we can do that.
 The stored procedure will do the recompilation (can't use same execution plan)
when we use the temp variable for every sub sequent calls. Where the table variable
won't do like that.
 Temporary Table structure can be changed after it’s creation it implies we can use
DDL statements ALTER, CREATE, DROP. Table Variables doesn’t support DDL
statements like ALTER, CREATE, DROP etc, implies we can’t modify the structure of
Table variable nor we can drop it explicitly.

Syntax to create the temporary variable

Create Table Variable


DECLARE @Customer TABLE
(
Id INT,
Name VARCHAR(50)
)
--Insert Two records
INSERT INTO @Customer
VALUES(1,'Basavaraj')
INSERT INTO @Customer
VALUES(2,'Kalpana')
--Reterive the records
SELECT * FROM @Customer
GO

You might also like