Advertisement
Beginner SQL Server, Temporary Tables & Table Variables

What is a local temporary table in SQL Server, and how is it different from a regular permanent table?

Short answer

A local temporary table, created with a name starting with a single pound sign, is automatically dropped when the session that created it ends, and is only visible to that specific session, making it useful for storing intermediate results during a complex process without…

Save SQL Server mock interview
Advertisement
AModel answer
A local temporary table, created with a name starting with a single pound sign, is automatically dropped when the session that created it ends, and is only visible to that specific session, making it useful for storing intermediate results during a complex process without permanently affecting the database's schema.
CREATE TABLE #TempOrders (
  OrderId INT,
  Amount DECIMAL(10,2)
);

INSERT INTO #TempOrders SELECT OrderId, Amount FROM Orders WHERE Status = 'Pending';
Real-world example
A complex reporting stored procedure stores an intermediate filtered set of pending orders in a local temporary table, using it for several subsequent calculations before the temporary table is automatically cleaned up when the procedure finishes.
Likely follow-ups
  • What happens to a temporary table if the session that created it disconnects unexpectedly?
  • Can a temporary table have indexes created on it?
Related topics