sql server - Update a column of a table using another column of the same table -


i have 2 tables vehicle_table , vehicle_group_table

  • vehicle_table --> has columns --> vehicleid,groupname,groupid
  • vehicle_group_table --> has columns --> groupid,groupname

i want update vehicle_table's groupid column joining vehicle_group_table on common groupname column

update vehicle_table      set vehicle_table.groupid = vehicle_group_table.groupid     vehicle_table.groupname = vehicle_group_table.groupname 

but seems not working.

update   v set   groupid = vg.groupid   vehicle_table v   join   vehicle_group_table vg on v.groupname = vg.groupname 

you need correlate 2 tables via join. there other ways subqueries etc

note: don't use aliases in destination/target columns in set clause suggested in other answers. fails in sql server not in sql server 2012 think behaviour changed

for more complicated setup: sql update query using joins


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -