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

wolfie on 02/21/08


Tagged

select sql update


Versions (?)


Who likes this?

3 people have marked this snippet as a favorite

brent-man
localhorst
liqweed


Update from select


Published in: SQL 


  1. TO just SET TO a predetermined value (true, false, 1, 'foo', etc.)
  2.  
  3. UPDATE tblDestination
  4. SET tblDestination.col=value
  5. WHERE EXISTS (
  6. SELECT col2.value
  7. FROM tblSource
  8. WHERE tblSource.join_col=tblDestination.join_col
  9. AND tblSource.constraint=value
  10. )
  11.  
  12. More advanced (sets value TO value IN secondary SELECT statement):
  13.  
  14. UPDATE tblDestination
  15. SET tblDestination.col=(SELECT col2.value FROM tblSource WHERE tblSource.join_col=tblDestination.join_col AND tblSource.constraint=value)
  16. WHERE EXISTS (
  17. SELECT col2.value
  18. FROM tblSource
  19. WHERE tblSource.join_col=tblDestination.join_col
  20. AND tblSource.constraint=value
  21. )

Report this snippet 

You need to login to post a comment.