Msg 199, Level 15, State 1, Line 0
An INSERT statement cannot contain a SELECT statement that assigns values to a variable.
This error message appears when you try to use an INSERT statement that contains a SELECT statement, in which values are assigned to a variable.
The SQL statement cannot be parsed and further execution is stopped.
Error of the Severity level 15 are generated by the user and can be fixed by the SQL Server user. The statement cannot be executed this way. You cannot assign values to a variable in an INSERT statement that uses a SELECT statement.
All versions of SQL Server.
IF OBJECT_ID(‘tempdb..#t’) > 0
DROP TABLE #t
CREATE TABLE #t
DECLARE @i INT
INSERT INTO #t SELECT @i = 1;
GROUP BY 1
In the above example we try to assign a value to the variable @i in the SELECT part of the above INSERT statement. This raises the error.