Posted By

wolfie on 02/21/08


Tagged

select sql update


Versions (?)

Who likes this?

7 people have marked this snippet as a favorite

brent-man
localhorst
liqweed
salmanMd
nefd
ljayz
g8rpal


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.