×

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 WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with...

4 minutes read.

Where vs Having

Difference Between Where and Having The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause...

3 minutes read.

SQL Select Distinct

The SQL DISTINCT query is used to fetch unique values from the tables using the SELECT statement in the SQL. There may be a situation that arises when you want to...

4 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 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 Scalar Functions

TEACHER Table LCASE() Function The LCASE() function is used to return lower case values of specified column. Syntax: Select Lcase(column_name) from table_name; Example: Select Lcase(teacher_name) from teacher; Syntax: Select Lcase(column_name) from table_name [where condition]; Example: Select Lcase(teacher_name) from teacher where teacher_id=1; UCASE() Function The UCASE()...

1 minute read.

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

3 minutes read.

SQL Operators

Arithmetic Operators Arithmetic operators are +, -, *, /, % performs addition, subtraction, multiplication, division, modulo respectively. Example:   Select 100+222; Select salary+100 From teacher Where teacher_id=1; Following output screen shows different arithmetic operations. We...

2 minutes read.

SQL Cloning Tables

Cloning a Table: To create a copy of the table. To perform the operations, without affecting the actual table. Steps for creating a Cloning Table: Step 1: Empty Table Creation The syntax for Creating...

3 minutes read.

WEB SQL

What is Web SQL? In SQL we can create databases, read the data in the databases, insert the records into the databases, and delete the records from the databases. For storing...

4 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 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.

Save Point in SQL

In SQL, the classification is done into 4 languages. They are Data Definition Language (DDL)Data Manipulation Language (DML)Transaction Control Language (TCL)Data Control Language (DCL) Save Point falls under the Transaction Control Language....

4 minutes read.

SQL GROUP BY CLAUSE

The GROUP BY clause is used to arrange similar records into the groups in the Structured Query Language queries. The records are arranged with the help of functions which is...

4 minutes read.

How to use LIKE in SQL

In this SQL article, we will learn and understand how to use LIKE to the columns in the SQL tables. What is Like? Like is an operator in the SQL. It is...

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

SQL FULL JOIN

In this section, we will help you understand the concept of the SQL FULL Join clause with a few examples. The SQL FULL Join query is executed to display the integrated...

3 minutes read.

How to drop a column in SQL?

How to drop a column in SQL Introduction To delete a column from an already created table, one needs to use the ALTER command along with the DROP COLUMN clause. Syntax: ALTER TABLE tablename...

4 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.

How to use the BETWEEN operator in SQL

In this entire SQL article, we will understand and learn about the BETWEEN operator concept and how to use it in SQL. What is the BETWEEN operator in SQL? The Between operator...

4 minutes read.