×

SQL Temporary Tables

Temporary Table:

The Temporary tables are generally created in TempDB. If the last connection is terminated then the tables are deleted automatically. These are generally used to store and process the intermediate data and intermediate results, these tables are used to store the data temporarily.

There are two types of Temporary tables They are:

  • Local Temporary Table
  • Global Temporary Table

Local Temporary Table:

A local temporary table is available only for the created session. A temporary table is deleted automatically when the last connection is terminated. The temporary table is created by using a single “#” character.

The user can delete the temporary method using the “Drop” query. If the Temporary table is created in stored functions, the temporary tables are deleted automatically after completion of stored functions execution.

The syntax for Creating a Local Temporary Table:

CREATE TABLE #Tablename (column_name1 datatype1, column_name2 datatype2, column_name3 datatype3, ………….., column_namen datatypen);

The syntax for Inserting values into Local Temporary Tables:

INSERT INTO #Tablename values (column_value1, column_value2, column_value3, ………….., column_valuen), (column_value1, column_value2, column_value3, ………….., column_valuen), (column_value1, column_value2, column_value3, ………….., column_valuen), …….., (column_value1, column_value2, column_value3, ………….., column_valuen);

The syntax for Displaying all the values from Local Temporary Tables:

SELECT * from #Tablename;

Displaying column-wise values:

SELECT column_name from #Tablename;

EXAMPLES:

1) Create a Student table and insert the details of the table and display the values of Student Details.

Creating Student Table:

CREATE TABLE #Student (sid int, sname varchar(20), sage int, sgender varchar(20), Phonenumber int);

>Temporary Table Created

Inserting Student details into the Table:

INSERT INTO #Student (1, ‘Abhinav’, 22, ‘Male’, 9895678909);

>Row created

INSERT INTO #Student (2, ‘Ramya’, 24, ‘Female’, 6687654634);

>Row created

INSERT INTO #Student (3, ‘Preetham’, 21, ‘Male’, 9867546453);

>Row created

INSERT INTO #Student (4, ‘Nethranand’, 21, ‘Male’, 7675643423);

>Row created

INSERT INTO #Student (5, ‘Naveen’, 23, ‘Male’, 6567784532);

>Row created

INSERT INTO #Student (6, ‘Harshitha’, 22, ‘Female’, 9867546231);

 >Row created

INSERT INTO #Student (7, ‘Bindu’, 26, ‘Female’, 6563412768);

>Row Created

INSERT INTO #Student (8, ‘Nandhini’, 23, ‘Female’, 6785674839);

>Row Created

INSERT INTO #Student (9, ‘Hashish’, 22, ‘Male’, 9453215052);

>Row Created

INSERT INTO #Student (10, ‘Rahul’, 21, ‘Male’, 9998989898);

>Row Created

Displaying the Table with Student Details:

SELECT * from #Student;

Student

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

Displaying one column of the table i.e, the name of the students:

SELECT sname from #Student;

Student

sname
Abhinav
Ramya
Preetham
Nethranand
Naveen
Harshitha
Bindu
Nandhini
Hashish
Rahul

Displaying one or more columns of the table i.e, the name of the students:

SELECT sid, sname from #Student;

Student

sidsname
1Abhinav
2Ramya
3Preetham
4Nethranand
5Naveen
6Harshita
7Bindu
8Nandhini
9Hashish
10Rahul

Global Temporary Table:

    Global Temporary table is created by the “##” character before the table name. These are visible to all the connections of the table. These tables are deleted or dropped, when it is referencing the last connection of table or when the table is closed.

The syntax for Creating a Global Temporary Table:

CREATE TABLE ##Tablename (column_name1 datatype1, column_name2 datatype2, column_name3 datatype3, ………….., column_namen datatypen);

The syntax for Inserting values into Global Temporary Tables:

INSERT INTO ##Tablename values (column_value1, column_value2, column_value3, ………….., column_valuen), (column_value1, column_value2, column_value3, ………….., column_valuen), (column_value1, column_value2, column_value3, ………….., column_valuen), …….., (column_value1, column_value2, column_value3, ………….., column_valuen);

The syntax for Displaying all the values from Global Temporary Tables:

SELECT * from ##Tablename;

Displaying column-wise values:

SELECT column_name from ##Tablename;

      EXAMPLES:

2) Create an Employee table and insert the details of the table and display the values of Employee Details.

Creating Employee Table:

CREATE TABLE #Employee (eid int, ename varchar(20), eage int, egender varchar(20), Phonenumber int);

>Temporary Table Created

Inserting Employee details into the Table:

INSERT INTO ##Employee (1, ‘Abhinav’, 22, ‘Male’, 9895678909);

>Row created

INSERT INTO ##Employee (2, ‘Ramya’, 24, ‘Female’, 6687654634);

>Row created

INSERT INTO ##Employee (3, ‘Preetham’, 21, ‘Male’, 9867546453);

>Row created

INSERT INTO ##Employee (4, ‘Nethranand’, 21, ‘Male’, 7675643423);

>Row created

INSERT INTO ##Employee (5, ‘Naveen’, 23, ‘Male’, 6567784532);

>Row created

INSERT INTO ##Employee (6, ‘Harshitha’, 22, ‘Female’, 9867546231);

 >Row created

 INSERT INTO ##Employee (7, ‘Bindu’, 26, ‘Female’, 6563412768);

>Row Created

 INSERT INTO ##Employee (8, ‘Nandhini’, 23, ‘Female’, 6785674839);

>Row Created

INSERT INTO ##Employee (9, ‘Hashish’, 22, ‘Male’, 9453215052);

>Row Created

INSERT INTO #Employee (10, ‘Rahul’, 21, ‘Male’, 9998989898);

>Row Created

Displaying the Table with Student Details:

SELECT * from #Student;

Employee

eidenameeageegenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

Displaying one column of the table i.e, the name of the Employees:

SELECT ename from #Student;

Employee

ename
Abhinav
Ramya
Preetham
Nethranand
Naveen
Harshitha
Bindu
Nandhini
Hashish
Rahul

Displaying one or More columns of the table i.e, the name of Employee:

SELECT eid, ename from #Employee;

Student

eidename
1Abhinav
2Ramya
3Preetham
4Nethranand
5Naveen
6Harshita
7Bindu
8Nandhini
9Hashish
10Rahul

Related Topics

SQL Between Operator

SQL Between operator is a logical operator in the Structured Query Language. The Between operator is used to retrieve data within the range specified in the condition in the query. The...

7 minutes read.

SQL WHERE Statement

SQL WHERE Statement Introduction WHERE clause is used to include a condition while fetching data from tables.When you have to specify a condition that has to be obeyed while data is...

9 minutes read.

SQL NOT Operator

SQL NOT is a Boolean operator used with the WHERE clause. NOT operator shows the records if the expression is false. When we use the NOT operator, we fetch only...

7 minutes read.

SQL Create Database

This tutorial will help us understand how to CREATE a Database in SQL with a few examples. The first step for storing structured records in the database is creating the databases. We...

4 minutes read.

How to Update Table in SQL?

How to Update Table in SQL Introduction UPDATE query is used to update a record in a table. UPDATE is a DML command, which operates on the data of the table and not...

4 minutes read.

SQL TABLE

SQL TABLE Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as...

3 minutes read.

SQL CONSTRAINTS

SQL Constraints specifies the rules/limitations/restrictions for data present in table. SQL Constraints are specified at the time of table creation or after table creation using ALTER command. There are two...

5 minutes read.

SQL Commands

SQL commands are classified into four groups on the basis of their nature. DDL (Data Definition Language) DML (Data Manipulation Language) DCL (Data Control Language) DQL (Data Query Language) NOTE: This...

1 minute read.

Grant Command in SQL

What is DCL (Data Control Language)? Data Control Language (DCL) is a computer programming language with syntax intended to manage access to data kept in databases. It is a part of...

3 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.

SQL LOGICAL Operator

In this tutorial, we will understand the operator who falls under the logical operator in SQL with the help of examples. The SQL Logical Operator displays the query result in one...

5 minutes read.

Types of SQL JOIN

The SQL JOIN combines one or more than one tables based on their relationship. The SQL JOIN involves a parent table and a child table relationship. There are different types of...

10 minutes read.

How to delete a row in SQL

Introduction To delete or remove the unused/unwanted rows from a table, DELETE command is used in SQL.DELETE command removes the entire record from a table.The DELETE statement can delete one or...

6 minutes read.

SQL INSERT Table

In this tutorial, we will help you to understand and learn how to add records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

SQL SELECT AVG

In this tutorial, we will learn about the aggregate function name avg() function concept in SQL with the help of examples. The AVG() function is one of the aggregate functions in...

3 minutes read.

Drop vs Truncate in SQL

In this article, we will learn and understand the Drop and Truncate commands and the difference between these two commands. What is Drop Command? Drop is a Data Definition Language command in...

6 minutes read.

SQL Cast Operator

While writing SQL queries, we can see many values which are to be converted into another datatype for accessing them. In SQL this conversion is generally done by CAST function. CAST...

4 minutes read.

SQL SELECT WHERE Clause

In this SQL section, we will help you understand the concept of the SQL SELECT WHERE clause with a few examples. The WHERE clause in SELECT query displays those records as...

5 minutes read.

SQL SET Keyword

This article will provide you a good understanding of the Set keyword in Structured Query Language. What is the SET keyword? The SET keyword is used to specify values for the variables....

3 minutes read.

SQL Temporary Tables

Temporary Table: The Temporary tables are generally created in TempDB. If the last connection is terminated then the tables are deleted automatically. These are generally used to store and process the...

4 minutes read.