T-SQL Tutorial

SQL Declare variable table


Declare variable table

To declare a table variable, use the DECLARE keyword, then type the @variable_name and variable type table with columns.
To insert values into a table variable, uses the INSERT statement based on a SELECT statement.

Example

USE model;
GO
DECLARE @TableVariable table(course_name varchar(200), course_price int);
INSERT INTO @TableVariable (course_name, course_price)
SELECT name, price FROM Training_Course WHERE id in (1,2);
SELECT course_name, course_price FROM @TableVariable;
GO

Result:

course_namecourse_price
SQL200
T-SQL700