×

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

How to create a database in SQL?

How to create a database in SQL It is very necessary to create a database in order to store the data into the database.Database name must always be unique.SQL does not...

6 minutes read.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 minutes read.

DELETE VS DROP in SQL

This page contains all the information about the Delete command and Drop command concept and the difference between the DELETE and DROP commands in SQL. What is the DELETE command in...

3 minutes read.

SQL INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

4 minutes read.

What is SQL Injection?

Introduction to SQL Injection SQL injection is a vulnerability or a technique that might destroy the database of a website or a web application. It is one of the most widely...

4 minutes read.

SQL Queries

In a database, queries are used to request the result set of data from the table or action on the records. A Query can answer your simple or complicated question, perform...

5 minutes read.

How to remove duplicates in SQL

Introduction There are some specific rules that needs to be followed while creating the database objects. To improve the performance of a database, a primary key, clustered and non-clustered indexes, and...

9 minutes read.

SQL Tutorial for Beginners

SQL tutorial provides basic and advanced concepts of Structured Query Language and how you deploy SQL to work with a relational database system. Our SQL tutorial is designed for beginners...

3 minutes read.

SQL CASE

This page contains all the information about SQL CASE. The CASE is an If-Else type of logical query used in the statement. The CASE in Structured Query Language is similar...

5 minutes 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.

WHERE Clause vs HAVING Clause

The WHERE Clause and HAVING clause filter the records in the Structured Query Language queries. The main difference between the WHERE clause and the HAVING clause is the WHERE clause...

5 minutes read.

DML Commands in SQL

DML is an abbreviation of Data Manipulation Language. Data Manipulation Language commands in Structured Query Language manipulate the data in the database. DML commands are used to retrieve records, add records,...

4 minutes read.

SQL HAVING Clause

In this tutorial, we will understand the HAVING clause concept in SQL with the help of examples. The HAVING Clause in the SQL is used as we cannot use the WHERE...

3 minutes read.

SQL Auto Increment

As we all know, In SQL for unique identification, we assign a column with the primary key. But in some tables, we find difficult to differentiate a column as a...

5 minutes read.

SQL Handling Duplicate

Removing Duplicates using DISTINCT Keyword: By using the DISTINCT keyword in SQL we can remove duplicate characters from tables or databases. A table contains more duplicate values and duplicate values can cause...

4 minutes read.

SQL Injection

SQL injection is a technique, this may destroy the database. It is one type of hacking technique. SQL IN WEB PAGES: Injection occurs when we ask for input like an id or...

3 minutes read.

SQL Comparision Operator

The Comparison Operator compares different data of the Structured Query Language table and checks whether the data are the same, less than, greater than, less than, or greater than equal....

14 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 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 SELECT LIKE Operator

The SQL SELECT LIKE Operator tutorial helps us understand how to use the LIKE operator in the SELECT query with examples. The SQL SELECT LIKE Operator retrieves the records from the...

4 minutes read.