Advertisement
Intermediate SQL Server, Temporary Tables & Table Variables

What is the difference between a local temporary table and a global temporary table?

Short answer

A local temporary table, prefixed with a single pound sign, is only visible to the session that created it, while a global temporary table, prefixed with two pound signs, is visible to all sessions connected to the server, and only gets dropped once every session referencing it…

Save SQL Server mock interview
Advertisement
AModel answer
A local temporary table, prefixed with a single pound sign, is only visible to the session that created it, while a global temporary table, prefixed with two pound signs, is visible to all sessions connected to the server, and only gets dropped once every session referencing it has disconnected, making it useful for sharing temporary data across multiple connections.
CREATE TABLE ##SharedTempData (
  Id INT,
  Value VARCHAR(100)
);
-- This table is visible to any session until all referencing sessions disconnect
Real-world example
A batch processing system uses a global temporary table to share intermediate calculation results across several separate connections working together on the same overall task, something a local temporary table could not support.
Likely follow-ups
  • What are the risks of using a global temporary table in a busy, multi user environment?
  • How do you know when a global temporary table will actually be dropped?
Related topics