We Recommend

SQL Cookbook SQL Cookbook
Written in O'Reilly's popular Problem/Solution/Discussion style, the SQL Cookbook is sure to please. Anthony's credo is: "When it comes down to it, we all go to work, we all have bills to pay, and we all want to go home at a reasonable time and enjoy what's still available of our days." The SQL Cookbook moves quickly from problem to solution, saving you time each step of the way.


Posted By

mafro on 11/23/07


Tagged

update


Versions (?)


Who likes this?

3 people have marked this snippet as a favorite

vali29
brent-man
uisluu


Update Select


Published in: SQL 


Update one table by selecting from another. Avoids cursors.

  1. -- Update one table from another
  2.  
  3. USE db_name
  4.  
  5. UPDATE d
  6.  
  7. SET col3 = s.col3,
  8. col4 = s.col4
  9.  
  10. FROM tbl_src AS s, tbl_dest AS d
  11. WHERE ((s.col3 IS NOT NULL) AND (s.col4 IS NOT NULL)) AND
  12. ((s.pk1 = d.pk1) AND (s.pk2 = d.pk2))

Report this snippet 

Comments

RSS Icon Subscribe to comments
Posted By: Yeah on January 18, 2008

Not working in MySQL...

Posted By: mafro on March 20, 2008

Alternative for MS Access:

UPDATE tbldest INNER JOIN tblsrc ON tbldest.pk1 = tblsrc.pk1 AND tbldest.pk2 = tblsrc.pk2 SET tbldest.col3 = tblsrc.col3, tbldest.col4 = tblsrc.col4;

You need to login to post a comment.