• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 557
  • Last Modified:

Copy value across tables

I need to update rows of a table based on values in another. I've googled this all morning but cant find anything useful.

I have  Select statement where I can see all the records I need to update (below) but I cant seem to make an update query that updates only the records that are returned by the select statement.
Select tblA_CommonFeild, tblA_NeedsChanging, tblB_ReferenceFeild
From tblA, tblB
WHERE tblB_CommonFeild=tblA_CommonFeild AND tblB_ReferenceFeild = 'NewValue' AND tblA_NeedsChanging='OldValue'

Open in new window

  • 2
1 Solution
Jinesh KamdarCommented:
Try this.
UPDATE tblA INNER JOIN tblB ON tblA_CommonFeild = tblB_CommonFeild 
SET tblA.tblA_NeedsChanging = tblB.tblB_ReferenceFeild

Open in new window

Jinesh KamdarCommented:
Or you can try this as well.

SET tblA_NeedsChanging = (SELECT tblB_ReferenceFeild FROM tblB WHERE tblB_CommonFeild = A.tblA_CommonFeild)
WHERE EXISTS (SELECT 1 FROM tblB WHERE tblB_CommonFeild = A.tblA_CommonFeild)
iambagelsAuthor Commented:

i was trying to select from both tables
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now