SQL Server Table Variable
Last Updated :
03 Jun, 2024
SQL Server Table variable is a local variable that stores data temporarily, similar to the temporary table in SQL Server.
Tempdb database is used to store table variables.
How to Declare Table Variable in SQL Server
To declare a table variable in SQL Server, start the DECLARE statement.
The name of the table variable must start with at(@) sign. The TABLE keyword defines that the used variable is a table variable. After the TABLE keyword, define column names and datatypes of the table variable in SQL Server.
Syntax
SQL Server Table Variable Syntax is:
DECLARE @TABLEVARIABLE TABLE(column1 datatype, column2 datatype, columnN datatype)
SQL Server Table Variable Examples
Let’s look at some examples of the SQL Server Table Variables, and understand how to declare, update, and delete table variables.
Declare Table Variable in SQL Server Example
In this example, we will declare a Table variable and insert values in it.
Query:
DECLARE @WeekDays TABLE (Number INT, Day VARCHAR(40), Name VARCHAR(40))
INSERT INTO @WeekDays
VALUES
(1, 'Mon', 'Monday'),
(2, 'Tue', 'Tuesday'),
(3, 'Wed', 'Wednesday'),
(4, 'Thu', 'Thursday'),
(5, 'Fri', 'Friday'),
(6, 'Sat', 'Saturday'),
(7, 'Sun', 'Sunday')
SELECT * FROM @WeekDays;
Output:
Number |
Day |
Name |
1 |
Mon |
Monday |
2 |
Tue |
Tuesday |
3 |
Wed |
Wednesday |
4 |
Thu |
Thursday |
5 |
Fri |
Friday |
6 |
Sat |
Saturday |
7 |
Sun |
Sunday |
Update and Delete Commands for table variable in SQL Server
In this example, we will update and delete the data in the table variables.
Query:
DELETE @WeekDays WHERE Number=7;
UPDATE @WeekDays SET Name='Saturday is a holiday' WHERE Number=6 ;
SELECT * FROM @WeekDays;
Number |
Day |
Name |
1 |
Mon |
Monday |
2 |
Tue |
Tuesday |
3 |
Wed |
Wednesday |
4 |
Thu |
Thursday |
5 |
Fri |
Friday |
6 |
Sat |
Saturday is a holiday |
Important Points About SQL Server Table Variable
- A table variable in SQL Server is a local variable that stores data temporarily, similar to temporary tables.
- It provides all the properties of a local variable but with some limitations compared to temp or regular tables.
- Table variables can be used for operations like insert, update, delete, and select within a SQL script.
- Table variables are not available after the execution of a complete query, unlike temporary tables that persist after the query execution.
- The lifecycle of a table variable starts from its declaration and ends when the declaration batch or stored procedure goes out of scope.
- Table variables can be used in SELECT, INSERT, UPDATE, and DELETE statements within their scope.
- The INTO clause in a SELECT statement cannot be used to create and populate a table variable.
- The structure of a table variable cannot be changed after it has been declared.
- Indexes cannot be created on table variables using the CREATE INDEX statement.
Similar Reads
Global Variables in MS SQL Server
Global variables are pre-defined system variables. It starts with @@. It provides information about the present user environment for SQL Server. SQL Server provides multiple global variables, which are very effective to use in Transact-SQL. The following are some frequently used global variables - @
2 min read
How to Declare a Variable in SQL?
Variables in SQL are fundamental for building efficient and scalable database applications. They enhance the flexibility and efficiency of database queries by acting as placeholders for data. Understanding how to declare and use variables in SQL is crucial for writing dynamic and effective queries I
3 min read
Inline Table Valued Function in SQL Server
In SQL Server, an Inline Table-valued function (ITVF) is a user-defined function that returns a table as its result. Unlike a scalar function that returns a single scalar value, an Inline Table Valued Function returns a result set that can be used in a query just like a table. An Inline Table Valued
2 min read
User-Defined Variables
User-Defined Variables in are a way to store temporary values that can be used within a session. These variables are unique to a session and are not persistent across multiple sessions. They can be very useful for storing intermediate results or values that you want to reuse within a single session.
4 min read
SQL ALTER TABLE
The SQL ALTER TABLE statement is a powerful tool that allows you to modify the structure of an existing table in a database. Whether you're adding new columns, modifying existing ones, deleting columns, or renaming them, the ALTER TABLE statement enables you to make changes without losing the data s
5 min read
SQL CREATE TABLE
In SQL, creating a table is one of the most essential tasks for structuring your database. The CREATE TABLE statement defines the structure of the database table, specifying column names, data types, and constraints such as PRIMARY KEY, NOT NULL, and CHECK. Mastering this statement is fundamental to
5 min read
SQL RENAME TABLE
Renaming a table is a common and useful operation for database administrators and developers. It is especially useful when we need to correct a naming mistake, organize our database schema, or update the table name to reflect new business requirements. In this article, we will provide a detailed gui
6 min read
SQL CREATE VIEW Statement
The SQL CREATE VIEW statement is a very powerful feature in RDBMSs that allows users to create virtual tables based on the result set of a SQL query. Unlike regular tables, these views do not store data themselves rather they provide a way of dynamically retrieving and presenting data from one or ma
4 min read
Table operations in MS SQL Server
In a relational database, the data is stored in the form of tables and each table is referred to as a relation. A table can store a maximum of 1000 rows. Tables are a preferred choice as: Tables are arranged in an organized manner. We can segregate the data according to our preferences in the form o
2 min read
What is Temporary Table in SQL?
A temporary table in SQL is an important tool for maintaining intermediate results during query execution. They help store temporary data without affecting the underlying permanent tables. In this article, weâll explore temporary tables in SQL, their types (local vs. global), and how to use them eff
3 min read