UPDATE one table based on a condition in another

UPDATE one table based on a condition in another

Is it possible to update a field in one table depending on a condition of a field in another? If so how is this done?

    Requires Free Membership to View

    By submitting your registration information to SearchOracle.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchOracle.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

Yes, but the exact syntax will depend on your particular database system.

For example, in SQL Server:

update table1
   set fldX = T2.fldY
  from table1 T1
inner
  join table2 T2
    on T1.keyfld = T2.keyfld
 where T2.fldZ = 'foo'

... whereas in Microsoft Access:

update table1
inner 
  join table2 
    on table1.keyfld = table2.keyfld
   set table1.fldX = table2.fldY
 where table2.fldZ = 'foo'

Just another example of different databases implementing the same thing in different ways.

For More Information


This was first published in November 2003

Join the conversationComment

Share
Comments

    Results

    Contribute to the conversation

    All fields are required. Comments will appear at the bottom of the article.