Link to home
Start Free TrialLog in
Avatar of rivkamak
rivkamakFlag for United States of America

asked on

update a field based off of another table

I have 1 table with a list of car makes and an ID.
then I have a list of models with a column for carid that matches the first table.

I was wondering  how I can update the model table and replace all the ids with the name from the make table where the makeid matches the other table.

Would something like this work?
update carmodels set carmodels.makeid = carmakes.carname where carmodels.makeid = carmakes.carid

Open in new window

Avatar of ste5an
Flag of Germany image

The T-SQL syntax is

SET makeID = O.carName
FROM carmodels C
  INNER JOI carmakes M ON C.makeID = M.makeID;

Open in new window

But why want you do this? You can use a JOIN anytime you need this information. Also it is semantically wrong, cause an ID is not a name. Thus this kind of update is not a good idea. It introduces also redundancy, which is a unwanted situation in the relational model, because it opens the model to get inconsistent.
1) You would not want to do that.  If you do want to put the name in the other table, you should create a corresponding column for it, not put a name into an "id" column.
2) I doubt you could put the full name in an id column anywhere.  Presumably the id column is either numeric or much shorter than the name value anyway.
Avatar of rivkamak


Right now I am using 2 sql statmenets and I am trying to figure out how to combine it into 1.
select * from CarMakes where name= ' getname' and type='automobile'
select * from CarModels where makeid = '"  & (makeList.Fields.Item("carid").Value)  & "' order by model

can i do this with 1 join?
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial