Temporary Tables are created under the tempdb database.
Types of Temporary Tables:
- Global Temporary Table (represented by ##)
- Local Temporary Table (represented by #)
- Table Variable
1. Global Temporary Table
Global Temporary tables are created under the tempdb and will be represented by ##.
Suppose I am creating a Global Temporary table named employee, so it will be like ##employee.
We can use a Global Temporary table everywhere i.e. in a query, SP etc except in Functions.
When we use a Global Temporary Table in a Query the scope of the table is through all the active sessions in The Management Studio. We can create an index on the Global Temporary Table. We can use the nolock clause with a Global Temporary Table.
Syntax to create a Global Temporary Table:
create table ##employee
(
empid int identity(1,1),
empName varchar(100),
empAddress varchar(100)
)
2. Local Temporary Table
Local Temporary tables are created under the tempdb and will be represented by #.
Suppose I am creating a Local Temporary table named employee, so it will be like #employee.
We can use a Local Temporary table everywhere i.e. in a query, SP etc. except in Functions.
When we use a Local Temporary Table in a query the scope of the table is the active session only in The Management Studio. Where we have created a Local Temporary table or we are directly inserting into it. We can create an index on the Local Temporary Table. We can use the nolock clause with a Local Temporary Table.
Syntax to create a Local Temporary Table:
create table #employee
(
empid int identity(1,1),
empName varchar(100),
empAddress varchar(100)
)
3. Table Variable
The table variable is used like a variable where we declare a variable of type table.
Suppose I am declaring the table variable.
declare @employee Table
(
empid int identity(1,1),
empName varchar(100),
empAddress varchar(100)
)
The table variable is worked like a table.
The table variable we can use everywhere including functions. We can't create the indexes on a table variable.
We can't use the no lock clause with Table Variables.
Generally we use the above #1, #2 & #3 when we are dealing with performance because using this in a query improves the performance of a query.


Ravi KandelPosted Jul 14, 2016, 8:07 AM EDT
Thanks for sharing.
Basil HowitsonPosted Jun 27, 2016, 12:46 AM EDT
Thank you Nitin. As a novice to the development environment I needed to understand the use of temporary tables in SQL. Much appreciated!
Santhakumar MunuswamyPosted Jun 24, 2016, 8:35 PM EDT
Thank you for nice article
Kuppurasu NagarajTOP 100Posted Jun 23, 2016, 9:29 AM EDT
Nice Sharing..
Prasanna MuraliPosted Jun 23, 2016, 6:15 AM EDT
Nice one...
Vignesh ManiTOP 100Posted Jun 23, 2016, 4:24 AM EDT
Nice one
Atul KumarPosted Jun 22, 2016, 11:50 PM EDT
useful.
Debasis SahaTOP 100Posted Jun 22, 2016, 10:25 PM EDT
Good One..
Hari ShankerPosted Jun 22, 2016, 9:34 PM EDT
Nice article
Yashwant VishwakarmaPosted Jun 22, 2016, 8:26 PM EDT
Nice one, thanks for sharing !!
Prerana TiwariPosted Jun 22, 2016, 8:07 PM EDT
Nice article....
RajaPosted Jun 22, 2016, 8:06 PM EDT
Nice Share...
Guest UserPosted Jun 22, 2016, 6:46 PM EDT
thnx!
kalu singh raoPosted Jun 22, 2016, 9:44 AM EDT
Nice...