T-SQL Tutorial

Msg 1087 Level 16 - Must declare the table variable


On Transact SQL language the Msg 1087 Level 16 - Must declare the table variable mean that you declared a non-table variable, and you use it as a table in a selection.

Msg 1087 Level 16 Example:

Invalid statement

USE model;
GO
DECLARE @MyVariable varchar(250);
SET @MyVariable = 'abcdefghij';
SELECT * FROM @MyVariable;
GO

Message:

Msg 1087, Level 16, State 1, Line 5
Must declare the table variable "@MyVariable".


Correct statement

USE model;
GO
DECLARE @MyVariable table (name varchar(250));
INSERT INTO @MyVariable(name) values ('abcdefghij');
SELECT * FROM @MyVariable;
GO

Result:

abcdefghij

Other error messages: