What Is Table Data Type and Table Variable?


Angela Bailey

What Is Table Data Type and Table Variable?

Tables are an essential component of any relational database. They allow us to store and organize large amounts of data, making it easier to retrieve and manipulate information. In this article, we will explore two important concepts related to tables: table data types and table variables.

Table Data Type

A table data type is a user-defined data type that represents a structured collection of data. It allows you to define a table structure with specific columns and their data types. This can be useful in scenarios where you need to pass tabular data as a parameter or store it in a variable.

To create a table data type, you can use the CREATE TYPE statement in SQL Server:

    Column1 INT,
    Column2 VARCHAR(50),
    Column3 DATE

In the above example, we created a table data type called MyTableType. It has three columns: Column1, of type INT, Column2, of type VARCHAR(50), and Column3, of type DATE.

You can now use this table data type as a parameter in stored procedures or functions:

    @Data MyTableType READONLY
    -- Do something with the passed table variable

The above stored procedure takes an input parameter named @Data, which is of the table data type MyTableType. You can then perform operations on this table variable within the stored procedure.

Table Variable

A table variable is a variable that can hold a result set of data, similar to a temporary table. It allows you to store and manipulate data within its scope. Table variables are useful when you need to perform operations on small sets of data or when you want to avoid the overhead of creating and dropping temporary tables.

To declare a table variable, you can use the DECLARE statement in SQL Server:

DECLARE @MyTableVariable MyTableType;

In the above example, we declared a table variable named @MyTableVariable, which is of type MyTableType. This means that it will have the same structure as the table data type we created earlier.

You can now insert data into the table variable using the INSERT INTO statement:

INSERT INTO @MyTableVariable (Column1, Column2, Column3)
(1, 'John Doe', '2022-01-01'),
(2, 'Jane Smith', '2022-02-01');

The above code inserts two rows of data into the table variable. Each row consists of values corresponding to the columns defined in the table data type.

You can then query and manipulate the data in the table variable just like you would with any other table:

FROM @MyTableVariable
WHERE Column1 = 1;

In the above example, we retrieve all rows from the table variable where Column1 has a value of 1.


Table data types and table variables are powerful features in SQL Server that allow you to work with structured collections of data. They provide flexibility and performance benefits in various scenarios. By understanding how to create and use them effectively, you can enhance your database development skills and optimize your code.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy