The Muffin Man
The Muffin Man

Reputation: 20004

SQL INSERT with sub query

I have a table with 2 columns. I want to provide the 1st columns value but use a select statement to query another table to figure out the value that will go in the 2nd column of the first table.

Heres what I came up with but I know is wrong..

INSERT INTO VehicleModels_VehicleSubModels (VehicleModelId, VehicleSubModelYearId)
(SELECT @ModelId, VehicleSubModelYearId 
FROM VehicleSubYearIntermediate 
WHERE SubModelId=@SubModelId 
AND YearId=@YearId)

Essentially I want to provide the value for VehicleModelId through @ModelId, but it won't let me use it outside of the select statement.

Upvotes: 1

Views: 765

Answers (1)

AdaTheDev
AdaTheDev

Reputation: 147374

Try removing the brackets around the SELECT, as presumbably you're seeing an incorrect syntax error?

INSERT INTO VehicleModels_VehicleSubModels (VehicleModelId, VehicleSubModelYearId)
SELECT @ModelId,VehicleSubModelYearId 
FROM VehicleSubYearIntermediate 
WHERE SubModelId=@SubModelId 
    AND YearId=@YearId

Upvotes: 5

Related Questions