AppDividend
Latest Code Tutorials

SQL Temporary Tables Tutorial Example | Global and Local Tables

0

SQL Temporary Tables Tutorial Example | Global and Local Tables is today’s topic. Temporary tables are stored in the tempdb. Temporary tables work like a regular table in which you can perform the operations like select, insert, and delete as for the regular table.

If temporary table created inside the stored procedure, they are destroyed upon the completion of the stored procedure. A temporary table in SQL, as the name suggests, is the database table that exists temporarily on a database server.

The temporary table stores a subset of data from a standard table for a specified period.

SQL Temporary Tables Tutorial Example

Tables act like a data structure in SQL, which is used to store records in it. In SQL, there exist temporary tables which are useful when processing data, especially during transformation where the intermediate results are transient.

These tables get created in the TempDB and are automatically deleted when they are no longer used.

If temporary tables are created in the User-created database, then it will be deleted automatically when the current client session terminates.

There are two types of temporary Tables.

Local temporary tables

Local temporary tables are visible only to their creators during the same connection to the instance of SQL Server as when the tables were first created or referenced.

Local temporary tables are deleted after the user disconnects from an instance of SQL Server.

Local temporary tables are created using a CREATE TABLE statement with the table name prefixed with a single number sign (#table_name).

If the local temporary table created inside the stored procedure, it is dropped automatically when a stored procedure is finished.

This means that this local temporary table can be referenced only by nested stored procedures.

A local temporary table cannot be referenced by a stored procedure or application that called a stored procedure that created a local temporary table.

Global temporary tables

Global temporary tables are visible to any connection and any user after they are created, and are removed when all the users that are referencing a table disconnect from an instance of SQL Server.

Global temporary tables are created using a CREATE TABLE statement with the table name prefixed with a double number sign (##table_name).

Global temporary tables are automatically dropped when a session that created a table ends and the last active Transact-SQL statement (not session) referencing this table in other sessions end.

Creation of Temporary Table

Temporary tables are created just like the permanent tables, and the only difference is that the temporary keyword is added before the table keyword. Let’s clear this with an example. See the following query.

Create temporary table Students(Roll integer,Name varchar(255));

The table will be created. Now for inserting values following commands is used.

Insert into Students values(1,”Shubh”);
Insert into Students values(2,”Ankit”);

To see the contents, type the following query.

Select * from Students;

 

SQL Temporary Tables Tutorial Example | Permanent and Local Tables

When the user executes SHOW TABLES command, then the temporary table will not be present in the list, and if you log out of MySQL and after returning to MySQL if you issue select * from table_name command, then no data will be shown to the user.

Dropping Temporary Tables

It is deleted automatically when the client session terminates. For dropping temporary tables manually, DROP keyword is used. See the following query.

DROP table table_name;

 

Drop Table

Operations such as update, delete, modify, and remaining all operations can be done in temporary tables similar to permanent tables.

Note: Temporary Tables are available in MySQL version 3.23 onwards.

Another way to create temporary tables

Temporary tables are created just like the permanent tables, and the only difference is that the table name is preceded with “#.” Let’s clear this with an example.

Create table #Students(Roll integer,Name varchar(255));

The above statement will create a local temporary table in TempDB. Now, to insert values within the table.

Insert into #Students values(1,”Shubh”);
Insert into #Students values(2,”Ankit”);

The above Statements will Add values to the table if we want to check whether the values have been added or not.

Select * from #Students;

These all commands will work on Microsoft SQL Server Management Studio Not on MySQL.

Finally, SQL Temporary Tables Tutorial Example | Global and Local Tables article is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.