SQL Server TSQL Temporary Table

Temp tables in SQL Server: they are just like any normal tables except that they are created in the TempDB database. They persist until dropped, or until the connection that created them disappears. They are visible in the procedure that created them and any procedures that it calls. Just like normal tables and have primary keys, constraints, indexes and column statistics which are kept for the table which makes query execution really fast in most cases. Temp tables can be created in all versions of SQL Server including SQL Server Express Edition.

Temp tables have space assigned to them in the TempDB database and are generally accessed only from memory, unless the server is under memory pressure or the amount of data in the table is large.

It is most common using temporary tables is from within a stored procedure.

It is common to use Temp table is to store the results of a SELECT INTO statement and then Insert into… Select statement.

Different Types of Temporary Tables SQL Server provides two types of temp tables based scope of the table. They are:

1.Local Temp Table
2.Global Temp Table

1.Local Temp Table

Local temp tables are only available to the current connection of the user. They are automatically deleted when the user disconnects from instances. Local temporary table name is stared with hash “#” sign.
Global Temp Table

Global Temp Table can be created by any connection, like a permanent table it is then available to any user by any connection. It can only be deleted once all connections have been closed.Global Temporary tables name starts with a double hash “##”

Creating Temporary Table in SQL Server

Local Temporary Table

 

CREATE TABLE #LocalTempTable
(ID int,
Name varchar(30),
Address varchar(300))

It Can be used like below


insert into #LocalTempTable values ( 1, 'Vamshi','India');


CREATE TABLE ##NewGlobalTempTable
(ID int,
Name varchar(30),
Address varchar(300))

It Can be used like below

insert into #NewGlobalTempTablevalues ( 1, 'Vamshi','India');

All temporary tables can be seen under Tempdb in SQL server under tables while they are in use and are stored in TempDB.

Vamshi B

View Comments

Recent Posts

sql server detected logical consistency based error

Learn about SQL Server detecting logical consistency based issues and how to resolve them.

5 months ago

sql server error 1222

Learn about SQL Server error 1222 and how to resolve the lock request time out…

5 months ago

Microsoft SQL Server 2022 New Features

Discover the new features of Microsoft SQL Server 2022 and how they compare to previous…

5 months ago

SQL Server Error 1222 lock request time out period exceeded

SQL Server Error 1222 lock request time out period exceeded   Lock request time out…

5 months ago

SQL Server Error : 427, Severity: 20. Could not load the definition for constr

SQL Server Error : 427, Severity: 20. Could not load the definition for constraint ID…

10 months ago

SQL Server Error : 204, Severity: 20. Normalization error in node %ls.

SQL Server Error : 204, Severity: 20. Normalization error in node %ls.

10 months ago

This website uses cookies.