1

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.

1 Answer 1

5

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 
Sign up to request clarification or add additional context in comments.

3 Comments

The problem is that @ModelId doesn't come from the table in the select statement, it gets inserted based on a value of some asp.net control. I want to provide the value for VehicleModelId and just use the SELECT statement to provide the value for VehicleSubModelYearId.
That's what this is doing though....you pass in a value for @ModelId and that constant value will be used for each row returned by the SELECT and go into VehicleModelId.
Really? It seemed like it would check the other table for rows with a column that matched @ModelId. I found that it does work, I just had a problem with some other checks I was doing. Thanks for the clarification.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.