INSERT INTO dbo.VehiclesCategories ( VehicleId, CategoryId )
VALUES ( t1.ID , t2.ID)
SELECT t1.ID, t2.ID
FROM dbo.Vehicle t1
inner JOIN dbo.VehicleCategory t2 ON t1.Category = t2.Name
In the above SQL, I want to insert two values into a M:M join table. The SELECT statement works fine and I get the desired results. The INSERT statement throws the following error:
Msg 128, Level 15, State 1, Line 2
The name "t1.ID" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
How do I insert the two values (t1.ID and t2.ID) into the table?
That’s because in the context of the INSERT,
t1andt2, don’t exist.If you combine those two queries, I think it will do exactly what you want: