In this post explains how to use the LOCAL TEMPORARY TABLES in SQL Server (Transact-SQL) with syntax and examples.
LOCAL TEMPORARY TABLES are distinct within modules and embedded SQL programs within SQL Server sessions. LOCAL TEMPORARY TABLES are stored in tempdb and SQL Server automatically deletes these tables when they are no longer used.
The syntax to create a LOCAL TEMPORARY TABLE in SQL Server (Transact-SQL) is:
The name of the local temporary table that you wish to create. The name of the local temporary table starts with the # character.
The columns that you wish to create in the local temporary table. Each column must have a datatype. The column should either be defined as NULL or NOT NULL and if this value is left blank, the database assumes NULL as the default.
Let's look at an example of how to create a LOCAL TEMPORARY TABLE in SQL Server (Transact-SQL).
For example:
This example would create a LOCAL TEMPORARY TABLE called #employees in SQL Server which has 4 columns.
This #employees table is stored in tempdb and SQL Server will automatically delete this table when the SQL Server session no longer requires it.