What is a SQL Server Temporary Table?

Answer Posted / mohammadali.info

Temporary tables are a useful tool in SQL Server provided to allow for short term use of data. There are two types of temporary table in SQL Server, local and global.

Local temporary tables are only available to the current connection to the database for the current user and are dropped when the connection is closed. Global temporary tables are available to any connection once created, and are dropped when the last connection using it is closed.

Both types of temporary tables are created in the system database tempdb.

Temporary tables can be created like any table in SQL Server with a CREATE TABLE or SELECT..INTO statement. To make the table a local temporary table, you simply prefix the name with a (#). To make the table a global temporary table, prefix it with (##).

-- Create a local temporary table using CREATE TABLE
CREATE TABLE #myTempTable
(
DummyField1 INT,
DummyField2 VARCHAR(20)
)

-- Create a local temporary table using SELECT..INTO
SELECT
age AS DummyField1,
lastname AS DummyField2
INTO #myTempTable
FROM DummyTable

To make these into global temporary tables, just replace (#) with (##)

Is This Answer Correct ?    15 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is fill factor and pad index?

533


How to provide login information for a new odbc dsn?

534


What do I need to start working with sql studio? : sql server management studio

581


What is the meaning of sql server?

524


How to drop existing views from a database in ms sql server?

521






Explain nested trigger in sql?

580


what is a live lock? : Sql server database administration

484


Do you know what is lock escalation?

574


What do you understand by mirroring?

558


What is the purpose of floor function?

529


How to create a stored procedure with a statement block in ms sql server?

568


What are SSL and TSL protocols?

577


When is update_statistics command used?

572


How to update muliple row in single query?

603


What is the filtered index?

551