Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

DAY 8

 Temporary Tables
Temporary Tables
 Table Variables
Temporary tables

As its name indicates, temporary tables are used to store data temporarily and they can perform CRUD (Create,
Read, Update, and Delete), join, and some other operations like the persistent database tables.

Temporary tables are dropped when the session that creates the table has closed, or can also be explicitly
dropped by users. At the same time, temporary tables can act like physical tables in many ways, which gives us
more flexibility. Such as, we can create constraints, indexes, or statistics in these tables.

SQL Server provides two types of temporary tables according to their scope:
• Local Temporary Table
• Global Temporary Table

LOCAL TEMP Tables :


The accessible scope of local temporary tables is limited by the connection in which they were created.
In other words, the local temporary tables are visible for the duration of the connection and when the
session is closed the local temporary table are dropped automatically.
Local temporary tables can be created using the “CREATE TABLE” syntax but a single hashtag (#) sign
must be added in front of their names
Global Temp Tables

The global temporary tables are created using the CREATE TABLE statement and their names must be prefixed
with the double hashtag (##) sign. These tables can be accessed by all other sessions, unlike local ones. The
following code will create a global temporary table. SQL –Reference File

Syntax :
CREATE TABLE #student_temp(
RollNo int NOT NULL PRIMARY KEY,
CTEandTempTables.sql
StudentName varchar(255) NULL,
DegreeMajor varchar(255) NOT NULL,
DegreeYear varchar(255) NULL,
Society varchar(255) NULL);
Syntax :
CREATE TABLE ##suppliers_temp
( SupplierId numeric(10) NOT NULL,
SupplierName char(50) NOT NULL,
ContactName char(50)
);
Table Variable

Is a special data type used to store a result set for processing at a later time. Table is primarily used for
temporarily storing a set of rows that are returned as the table-valued function result set.
Functions and variables can be declared to be of type table.
Table variables can be used in functions, stored procedures, and batches.
To declare variables of type table, use DECLARE @local_variable.
Table variables are kinds of variables that allow you to hold rows of data, which are similar to
temporary tables

Syntax :

DECLARE @table_variable_name TABLE ( column_list );

Scope :
Table variables are out of scope at the end of the batch
If you define a table variable in a stored procedure or user-defined function, the table variable will no longer
exist after the stored procedure or user-defined function exits.
Create Syntax :
DECLARE @product_table TABLE (ProductName VARCHAR(MAX) NOT NULL,
BrandId INT NOT NULL,
ListPrice DEC(11,2) NOT NULL);

Insert Syntax :
INSERT INTO @product_table
SELECT ProductName, BrandId, ListPrice
FROM Production.Products WHERE CategoryId = 1;

Execute : Note that need to execute the whole batch with select Statement

Reference: https://www.sqlservertutorial.net/sql-server-basics/sql-server-temporary-tables/
https://www.mssqltips.com/sqlservertip/1556/
differences-between-sql-server-temporary-tables-and-table-variables/
Temp Table Table Variable
Table variable involves the effort when you usually create the
A Temp table is easy to create and back up data.
normal tables.
Temp table result can be used by multiple users. Table variable can be used by the current user only.
Temp table will be stored in the tempdb. It will make network Table variable will store in the physical memory for some of the
traffic. When you have large data in the temp table then it has to data, then later when the size increases it will be moved to the
work across the database. A Performance issue will exist. tempdb.
Temp table can do all the DDL operations. It allows creating the Table variable won't allow doing the DDL operations. But the
indexes, dropping, altering, etc.. table variable allows us to create the clustered index only.
Temp table can be used for the current session or global. So Table variable can be used up to that program. (Stored
that a multiple user session can utilize the results in the table. procedure)
When we do the DML operations with the temp table then it can Table variable cannot use the transactions. But we cannot
be rollback or commit the transactions. rollback or commit for table variable.
Functions cannot use the temp variable. More over we cannot
do the DML operation in the functions but using the table
Function allows us to use the table variable.
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 Where the table variable won't do like that.
subsequent calls.
Exercise
• What are the differences between Local and Global Temporary Table ?
• Can you TRUNCATE the temp table ?
• Can you access a temp table created by a stored procedure in the same connection after executing
the stored procedure?
• Can you insert the IDENTITY Column value in the temp table
• Can you create a Table variable schema by using the SELECT INTO command?
• Can you use the User Defined Type in the Table Variable ?
• What is the scope of the Table Variable in SQL ?
• Demonstrate use of DML commands (INSERT, UPDATE, DELETE) on the Temp tables in SQL?
• Can you use DML commands (INSERT, UPDATE, DELETE) on the Table Variable in SQL?

SQL exercises on TEMPORARY TABLES AND TABLE VARIABLES (wiseowl.co.uk)

You might also like