---
title: "What is the difference between a temp table and a variable table in SQL Server?"  
description: "What is the difference between a temp table and a variable table in SQL Server?"  
author: "Revati S Misra"  
published: 2023-04-27  
updated: 2023-07-02  
canonical: https://www.mindstick.com/forum/158046/what-is-the-difference-between-a-temp-table-and-a-variable-table-in-sql-server  
category: "mssql server"  
tags: ["database", "sql server", "sql"]  
reading_time: 2 minutes  

---

# What is the difference between a temp table and a variable table in SQL Server?

What is the [difference](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is) between a [temp table](https://www.mindstick.com/forum/156791/how-to-create-temp-table-for-stored-procedure-in-sql-server) and a [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) [table in SQL](https://www.mindstick.com/forum/205/find-the-all-column-with-schema-for-any-table-in-sql-server) [Server](https://www.mindstick.com/articles/43769/what-is-serverless-architecture-is-it-worth-switching-over)?

## Replies

### Reply by Aryan Kumar

Sure. A **temporary [table](https://www.mindstick.com/articles/43918/how-to-design-table-using-bootstrap)** and a **variable table** are both types of in-memory tables in [SQL Server](https://www.mindstick.com/articles/12999/what-is-table-valued-function-in-sql-server). However, they have some key differences.

- **Temporary tables** are stored in the **tempdb** database, which is a special database that is created when SQL Server starts up. Temporary tables are available to all users of the database server.
- **Variable tables** are stored in the **memory** of the **current user**. Variable tables are only available to the user who created them.

Temporary tables are **persistent**, which means that they remain in memory until the **transaction** that created them is **committed** or **rolled back**. Variable tables are **non-persistent**, which means that they are destroyed when the **current batch** or **stored procedure** is **completed**.

Temporary tables can be **indexed**, while variable tables cannot. Temporary tables can also be **used in joins**, while variable tables cannot.

Here is a table that summarizes the key differences between temporary tables and variable tables:

| Feature | Temporary table | Variable table |
| --- | --- | --- |
| Storage location | **tempdb** database | **Memory** of the **current user** |
| Persistence | Persistent | Non-persistent |
| Indexing | Can be indexed | Cannot be indexed |
| Joining | Can be used in joins | Cannot be used in joins |

Which one you should use will depend on your specific needs. If you need to create a temporary table that can be used by multiple users, or if you need to create a temporary table that can be indexed or used in joins, then you should use a temporary table. If you only need to create a temporary table that is available to the current user, and if you do not need to index it or use it in joins, then you should use a variable table.


---

Original Source: https://www.mindstick.com/forum/158046/what-is-the-difference-between-a-temp-table-and-a-variable-table-in-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
