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_name | course_price |
---|---|
SQL | 200 |
T-SQL | 700 |