FeatureLocal Temp Table (#Temp)Global Temp Table (##Temp)
ScopeSession-specificAvailable to all sessions
VisibilityOnly visible to the session that created itVisible to all sessions after creation
NamingPrefix with single #, e.g., #UsersPrefix with double ##, e.g., ##Users
LifetimeDropped automatically when the session ends or explicitly droppedPersists until the last session using it is closed or explicitly dropped
Use CaseTemporary storage for one user’s session (e.g., stored procedures, intermediate processing)Sharing temporary data between multiple sessions or users
IsolationIsolated per session; multiple users can create their own #Temp table with the same nameOnly one global temp table with the same name can exist at a time

🧪 Examples

1. Local Temp Table (#Temp)

-- Session 1
CREATE TABLE #Employees (
    ID INT,
    Name NVARCHAR(50)
);
 
INSERT INTO #Employees VALUES (1, 'John'), (2, 'Alice');
 
SELECT * FROM #Employees;

❗ This table is only accessible in this session. It will be dropped automatically when the session closes.

2. Global Temp Table (##Temp)

-- Session 1
CREATE TABLE ##GlobalEmployees (
    ID INT,
    Name NVARCHAR(50)
);
 
INSERT INTO ##GlobalEmployees VALUES (1, 'Bob'), (2, 'Eve');
-- Session 2 (can run in SSMS in another window)
SELECT * FROM ##GlobalEmployees;

✅ This table is accessible from other sessions as long as the creating session is still active or until explicitly dropped.


⚠️ Tips

  • Temp tables are stored in the tempdb system database.

  • Use DROP TABLE #Temp or DROP TABLE ##Temp to manually clean up when done.

  • Avoid name collisions with global temp tables since only one can exist at a time.