Skip to the content.

There are two kinds of SQL variables; scalar variables that hold a single typed value, and table-variables that work like temporary-tables, but with very specific scoping rules. You cannot treat a table-variable like a scalar; you can’t use set/select assignment, select the value (rather than columns from the value), etc.

Bad:

declare @t table (Id int not null);
insert @t (Id) values (42);
select @t;

Good:

declare @t table (Id int not null);
insert @t (Id) values (42);
select Id from @t;